使用Java blogger API v3动态发布在博客上

关于使用Java blogger API v3动态地将post发布到我的博客帐户,我有两个问题。

第一

我使用以下代码获取访问我的博客的凭据:

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个post之后(注意我在连续请求之间设置等待大约5秒),我开始从api调用中收到以下错误:

 { "code" : 403, "errors" : [ { "domain" : "usageLimits", "message" : "Rate Limit Exceeded", "reason" : "rateLimitExceeded" } ], "message" : "Rate Limit Exceeded" } 

我回到我的配额页面,我发现我发送的请求并没有从我每天允许的请求中减少!

我的第二个问题是:

我是否忘记了动态正确操作博客的特定配置?

提前感谢您的帮助和支持。

没有办法为Blogger预先授权某人,所以我认为访问Blogger Api的唯一方法是通过Auth 2 Playground生成访问令牌,然后使用令牌进行API调用。

即使它在控制台上显示10000个请求/天和1个请求/秒/用户的限制,但事实是Blogger api默认情况下每天最多允许50个请求。 有一段时间,有条款要求额外的配额,通过指定现已停止的真正需求。

您可以通过代码生成api令牌,而不是从操场上生成它。 但是,您必须在第一时间进行身份validation。

 private static GoogleCredential getCredentials(HttpTransport httpTransport, JacksonFactory jacksonFactory, List 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