Java – sessionexpiredexception occurs when trying to create a post using Google blogger API oauth2
•
Java
I use the following code to update an existing blog post I received sessionexpiredexception What on earth did I do wrong?
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);
Output:
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)
I also tried the following methods
Method 2:
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);
Method 3:
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);
But I got the same exception
Solution
It seems that the API of blog is not supported We must use V3 API
Import jars after download: http://developers.google.com/blogger/docs/3.0/api-lib/java
Then follow the answer: using java blogger API V3 to post on blog dynamically
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码