关于使用Java 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 ......我设置了从以下google页面生成的访问令牌(credential.setAccessToken):https://developers.google.com/oauthplayground
但是这个令牌每3600秒就过期一次。因此,我再次访问该页面,并按下按钮“刷新访问令牌”以获得另一个令牌,并在上面的代码中再次使用它。
是访问我的博客并以编程方式动态发布内容和文章的正确方法吗?
第二
在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"
}我返回到我的配额页面,我看到我发送的请求并没有从我每天允许的请求中减少!
我的第二个问题是:
,我是否忘记了正确动态地操作博客的特定配置?
提前感谢您的帮助和支持。
发布于 2015-09-29 20:02:07
没有办法为Blogger预先授权某个人,所以我认为访问Blogger Api的唯一方法是通过8月2日Playground生成访问令牌,然后使用令牌进行API调用。
尽管它在控制台上显示了10000个请求/日和1个请求/秒/用户,但事实上,默认情况下,Blogger api每天只允许最多50个请求。前一段时间,已有规定要求增加配额,具体说明对配额的真正需要,而该规定现已停止。
发布于 2015-11-23 21:32:36
您可以通过代码生成api令牌,而不是从操场生成它。但是,您必须在第一次验证。
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("xyz@gmail.com")
.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());
}v3客户端库的JARs:http://developers.google.com/blogger/docs/3.0/api-lib/java
https://stackoverflow.com/questions/27115446
复制相似问题