尝试使用 Google Blogger API OAuth2 创建 post 时发生 SessionExpiredException
SessionExpiredException occured when trying to create a post using Google Blogger API OAuth2
我正在使用以下代码更新现有博客 post。我收到 SessionExpiredException。我做错了什么?
GoogleService service = new BloggerService("MyBloggerIntegration-v1");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
Arrays.asList("https://www.googleapis.com/auth/blogger"))
.setAccessType("offline")
.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());
service.setOAuth2Credentials(new GoogleCredential().setFromTokenResponse(response));
Entry myEntry = new Entry();
myEntry.setTitle(new PlainTextConstruct(title));
myEntry.setContent(new PlainTextConstruct(content));
URL postUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default/"+postID);
service.update(postUrl, myEntry);
输出:
Please open the following URL in your browser then type the authorization code:
https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=auto&client_id=67330569820-unio63db63ljnloc1hd52bvcoj8g8vrr.apps.googleusercontent.com&redirect_uri=http://cegcodingcamp.blogspot.in&response_type=code&scope=https://www.googleapis.com/auth/blogger
4/d_1GaJe4lHpOdRmhidwWgC7_utKoqfbwXnfYoP2hR_c#
Response : {
"access_token" : "ya29.MQJqQi7HqTcJGoCCU-Lo5Ybdb1Otc-Z_fsAN97oySVsU84A7IXr_cPqWcrMe2raZoSvU",
"expires_in" : 3591,
"token_type" : "Bearer"
}
Exception in thread "main" com.google.gdata.client.GoogleService$SessionExpiredException: Unauthorized
User does not have permission to edit object
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)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.Service.update(Service.java:1563)
at com.google.gdata.client.Service.update(Service.java:1530)
at com.google.gdata.client.GoogleService.update(GoogleService.java:604)
at line service.update(postUrl, myEntry)
我也尝试了以下方法
方法二:
service = new GoogleService("blogger", "exampleCo-exampleApp-1");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
com.google.api.client.json.jackson2.JacksonFactory jacksonFactory = com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance();
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jacksonFactory)
.setServiceAccountId("xxxx.apps.googleusercontent.com")
.setServiceAccountPrivateKeyFromP12File(new File("resources/xxxx.p12"))
.setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/blogger"))
.build();
service.setOAuth2Credentials(credential);
方法三:
service = new GoogleService("blogger", "exampleCo-exampleApp-1");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance();
List<String> collection = new ArrayList<String>();
collection.add("https://www.googleapis.com/auth/blogger");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("xxxx.apps.googleusercontent.com")
.setServiceAccountPrivateKeyFromP12File(
new File("resources\xxxx.p12"))
.setServiceAccountScopes(collection)
.build();
credential.setAccessToken("ya29.MQJqQi7HqTcJGoCCU-Lo5Ybdb1Otc-Z_fsAN97oySVsU84A7IXr_cPqWcrMe2raZoSvU");
service.setOAuth2Credentials(credential);
但我遇到了同样的异常。
似乎不支持 Blogger API 的 V2。我们必须使用 V3 API.
下载后导入 Jars : http://developers.google.com/blogger/docs/3.0/api-lib/java
然后按照这个答案:Using Java blogger API v3 to post on blog dynamically
我正在使用以下代码更新现有博客 post。我收到 SessionExpiredException。我做错了什么?
GoogleService service = new BloggerService("MyBloggerIntegration-v1");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance();
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
Arrays.asList("https://www.googleapis.com/auth/blogger"))
.setAccessType("offline")
.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());
service.setOAuth2Credentials(new GoogleCredential().setFromTokenResponse(response));
Entry myEntry = new Entry();
myEntry.setTitle(new PlainTextConstruct(title));
myEntry.setContent(new PlainTextConstruct(content));
URL postUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default/"+postID);
service.update(postUrl, myEntry);
输出:
Please open the following URL in your browser then type the authorization code:
https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=auto&client_id=67330569820-unio63db63ljnloc1hd52bvcoj8g8vrr.apps.googleusercontent.com&redirect_uri=http://cegcodingcamp.blogspot.in&response_type=code&scope=https://www.googleapis.com/auth/blogger
4/d_1GaJe4lHpOdRmhidwWgC7_utKoqfbwXnfYoP2hR_c#
Response : {
"access_token" : "ya29.MQJqQi7HqTcJGoCCU-Lo5Ybdb1Otc-Z_fsAN97oySVsU84A7IXr_cPqWcrMe2raZoSvU",
"expires_in" : 3591,
"token_type" : "Bearer"
}
Exception in thread "main" com.google.gdata.client.GoogleService$SessionExpiredException: Unauthorized
User does not have permission to edit object
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)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.Service.update(Service.java:1563)
at com.google.gdata.client.Service.update(Service.java:1530)
at com.google.gdata.client.GoogleService.update(GoogleService.java:604)
at line service.update(postUrl, myEntry)
我也尝试了以下方法
方法二:
service = new GoogleService("blogger", "exampleCo-exampleApp-1");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
com.google.api.client.json.jackson2.JacksonFactory jacksonFactory = com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance();
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jacksonFactory)
.setServiceAccountId("xxxx.apps.googleusercontent.com")
.setServiceAccountPrivateKeyFromP12File(new File("resources/xxxx.p12"))
.setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/blogger"))
.build();
service.setOAuth2Credentials(credential);
方法三:
service = new GoogleService("blogger", "exampleCo-exampleApp-1");
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = com.google.api.client.json.jackson2.JacksonFactory.getDefaultInstance();
List<String> collection = new ArrayList<String>();
collection.add("https://www.googleapis.com/auth/blogger");
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("xxxx.apps.googleusercontent.com")
.setServiceAccountPrivateKeyFromP12File(
new File("resources\xxxx.p12"))
.setServiceAccountScopes(collection)
.build();
credential.setAccessToken("ya29.MQJqQi7HqTcJGoCCU-Lo5Ybdb1Otc-Z_fsAN97oySVsU84A7IXr_cPqWcrMe2raZoSvU");
service.setOAuth2Credentials(credential);
但我遇到了同样的异常。
似乎不支持 Blogger API 的 V2。我们必须使用 V3 API.
下载后导入 Jars : http://developers.google.com/blogger/docs/3.0/api-lib/java
然后按照这个答案:Using Java blogger API v3 to post on blog dynamically