我使用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"
}
我找不到问题。为什么我不能在博客上发帖?
我知道这是一个非常古老的问题,但我最近试图用它的Java客户端发布一篇博客文章,并写了一篇关于这个的博客:从它的Java客户端使用blogger API
我认为您需要使用OAuth身份验证而不是服务帐户