使用java发布到Blogger

时间:2015-10-01 09:01:35

标签: java api post google-api blogger

我使用Blogger API发布到Blogger。但是出现了问题。

我的计划:

try {
        HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
        JsonFactory JSON_FACTORY = new JacksonFactory();
        File file = new File("ExampleMyProject-0000ee0000.p12");

        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(HTTP_TRANSPORT)
                .setJsonFactory(JSON_FACTORY)
                .setServiceAccountId("00000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxx77l6@developer.gserviceaccount.com")
                .setServiceAccountScopes(Arrays.asList("https://www.googleapis.com/auth/blogger"))
                .setServiceAccountPrivateKeyFromP12File(file).build();
        credential.refreshToken();
        String accessToken = credential.getAccessToken();

        System.out.println(accessToken);

        Blogger blogger = new Blogger.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName("ExampleQuintest").build();
        Post content = new Post();
        content.setTitle("A test post");
        content.setContent("With <code>HTML</code> content");

        Insert postsInsertAction = blogger.posts().insert("MyBloggerID", content);
        postsInsertAction.setFields("author/displayName,content,published,title,url");

        Post post = postsInsertAction.execute();

        System.out.println("Title: " + post.getTitle());
        System.out.println("Author: " + post.getAuthor().getDisplayName());
        System.out.println("Published: " + post.getPublished());
        System.out.println("URL: " + post.getUrl());
        System.out.println("Content: " + post.getContent());

    } catch (GeneralSecurityException | IOException e) {
        e.printStackTrace();
    }

响应值:

{
  "code" : 403,
   "errors" : [ {
    "domain" : "global",
    "message" : "We're sorry, but you don't have permission to access this resource.",
     "reason" : "forbidden"
  } ],
   "message" : "We're sorry, but you don't have permission to access this resource."
}

https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=[MyAccessToken]

{
 "issued_to": "xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
 "audience": "xxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com",
 "scope": "https://www.googleapis.com/auth/blogger",
 "expires_in": 2569,
 "access_type": "offline"
}

我找不到问题。为什么我不能发布到博客?

1 个答案:

答案 0 :(得分:0)

我知道这是一个非常老的问题,但是我最近尝试使用其Java客户端发布博客文章,并为此写了博客:Use Blogger API from its Java client

我相信您需要使用OAuth身份验证而不是服务帐户。