使用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)
获取访问令牌然后进行帖子调用以插入新帖子的正确和有效方法是什么?如果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();
}