关于使用 Java blogger API v3 将帖子动态发布到我的博客帐户,我遇到两个问题。
第一
我使用以下代码来获取访问我的博客的凭据:
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(emailAddress)
.setServiceAccountPrivateKeyFromP12File(
new File(p12FileLocation))
.setServiceAccountScopes(Collections.singleton(BloggerScopes.BLOGGER))
.build();
credential.setAccessToken("zRLqmkM82626Uym9Uv1Jsdd");
Blogger blogger = new Blogger.Builder(httpTransport, JSON_FACTORY, credential)
.setApplicationName("Blogger")
.build();
// .... rest of the code to prepare post and send it ......
我设置了上面的访问 token (credential.setAccessToken),该 token 是从以下 google 页面生成的:https://developers.google.com/oauthplayground
但此 token 每 3600 秒就会过期。因此我再次访问该页面并按“刷新访问 token ”按钮以获取另一个 token 并在上面的代码中再次使用它。
这是访问我的博客并以编程方式动态发布内容和文章的正确方法吗?
第二
在谷歌开发者控制台中https://developers.google.com/console我看到我每天有 10000 个请求,限制为 1 个请求/秒/用户
但是
使用上面的代码正确动态发布大约 50 个帖子后(请注意,我在连续请求之间设置了大约 5 秒的等待),我开始从 api 调用收到以下错误:
{
"code" : 403,
"errors" : [ {
"domain" : "usageLimits",
"message" : "Rate Limit Exceeded",
"reason" : "rateLimitExceeded"
} ],
"message" : "Rate Limit Exceeded"
}
我返回到我的配额页面,我发现我发送的请求并没有比每天允许的请求减少!!!
我的第二个问题是:
我是否忘记了正确动态操作我的博客的特定配置?
预先感谢您的帮助和支持。
最佳答案
您可以通过代码生成 api token ,而不是从 Playground 生成。但是您必须在第一次进行身份验证。
private static GoogleCredential getCredentials(HttpTransport httpTransport, JacksonFactory jacksonFactory,
List<String> scopes) throws IOException, GeneralSecurityException {
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jacksonFactory,
CLIENT_ID, CLIENT_SECRET, scopes).setAccessType("online").setApprovalPrompt("auto").build();
String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();
System.out.println("Please open the following URL in your " + "browser then type the authorization code:");
System.out.println(" " + url);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String code = br.readLine();
GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();
System.out.println("Response : " + response.toPrettyString());
GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
.setJsonFactory(jacksonFactory).setServiceAccountId("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="641c1d1e240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>")
.setServiceAccountPrivateKeyFromP12File(new File("resources\\xyz.p12"))
.setServiceAccountScopes(Collections.singleton(BloggerScopes.BLOGGER)).build();
credential.setAccessToken(response.getAccessToken());
return credential;
}
public static Blogger getBlog() throws IOException, GeneralSecurityException, AuthenticationException {
if (blog == null) {
if (httpTransport == null)
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
if (jacksonFactory == null)
jacksonFactory = JacksonFactory.getDefaultInstance();
blog = new Blogger.Builder(httpTransport, jacksonFactory,
getCredentials(httpTransport, jacksonFactory, Arrays.asList(BloggerScopes.BLOGGER)))
.setApplicationName("Blogger").build();
}
return blog;
}
public static void udpatePost(String title, String content) throws IOException, AuthenticationException, GeneralSecurityException{
Post post = new Post();
post.setTitle(title);
post.setContent(content);
Update updateAction = getBlog().posts().update(BLOG_ID, POST_ID, post);
updateAction.setFields("author/displayName,content,published,title,url");
post = updateAction.execute();
System.out.println("Published: " + post.getPublished());
}
API v3 客户端库的 JAR:http://developers.google.com/blogger/docs/3.0/api-lib/java
关于java - 使用 Java blogger API v3 在博客上动态发帖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27115446/