关于使用 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 ......
我在上面设置了从以下谷歌页面生成的访问令牌(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"
}
我返回到配额页面,看到我发送的请求没有减少到每天允许的请求数!!
我的第二个问题是:
我是否忘记了正确动态操作博客的特定配置?
提前感谢您的帮助和支持。
您可以通过代码生成 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());
}
API v3 客户端库的 JAR:http://developers.google.com/blogger/docs/3.0/api-lib/java
没有办法为 Blogger 预先授权某人,所以我相信访问 Blogger API 的唯一方法是通过 Auth 2 Playground 生成访问令牌,然后将令牌用于 API 调用。
尽管它在控制台上显示 10000 个请求/天和 1 个请求/秒/用户的限制,但事实是 Blogger API 默认每天最多只允许 50 个请求。直到不久前,还有规定通过具体说明对配额的真正需求来要求增加配额,现在已经停止。