Google Blogger API - 未经授权的用户无权创建新帖子

时间:2015-09-28 01:45:14

标签: google-api blogger google-api-java-client

使用Google Blogger API插入新帖子时出现以下错误。

com.google.gdata.client.GoogleService$SessionExpiredException: Unauthorized
User does not have permission to create new post

at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:570)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
  • 使用服务帐户,OAuth2和GoogleCredential进行生成 的accessToken。
  • accessToekn成功创建。
  • 在某处我读到谷歌不支持服务帐户 Blogger API。
  • 因此尝试使用Client Id和Client Secret来获取可以的代码 再次用于获取使用范围的访问令牌
    " https://www.googleapis.com/auth/blogger&#34 ;.
  • 在执行此操作时,我收到的错误是"不是有效范围"。

获取访问令牌然后进行帖子调用以插入新帖子的正确和有效方法是什么?如果Blogger不允许博客API的服务帐户,我需要在获取访问令牌之前对请求进行身份验证,这对我也有用。我只需要一种机制来为Blogger API生成有效的访问令牌,稍后我就可以插入帖子。

目前我的代码如下所示。感谢任何帮助。感谢。

     String clientId = "xxxx.apps.googleusercontent.com";
        String emailAddress = "xxxxx@developer.gserviceaccount.com";
        JsonFactory JSON_FACTORY = new JacksonFactory();
        HttpTransport httpTransport = null;
        try {
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        }

 catch (GeneralSecurityException e1) {
        e1.printStackTrace();
    }

File file = new File("C://xxxx/IQ/PostBlogPost-d6158daa91a5.p12");

GoogleCredential credential = null;

try {
    credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(emailAddress)
            .setServiceAccountPrivateKeyFromP12File(file)
.setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/blogger")).build();
        } catch(Exception ex){

        }

credential.refreshToken();

BloggerService myService = new BloggerService("exampleCo-exampleApp-1");
        myService.setAuthSubToken(credential.getAccessToken());
        myService.setOAuth2Credentials(credential);


try {
 Entry myEntry = new Entry();
 myEntry.setTitle(new PlainTextConstruct("test-title"));
 myEntry.setContent(new PlainTextConstruct("xxxxxxx"));

 // Ask the service to insert the new entry
 URL postUrl = new URL("http://www.blogger.com/feeds/" + "xxxx" + "/posts/default");
  myService.insert(postUrl, myEntry);
 } catch (ServiceException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
 }

0 个答案:

没有答案