java – 尝试使用Google Blogger API OAuth2创建帖子时发生Sessi
发布时间:2020-12-15 02:11:31 所属栏目:Java 来源:网络整理
导读:我使用以下代码更新现有的博客文章.我收到了SessionExpiredException.我究竟做错了什么? GoogleService service = new BloggerService("MyBloggerIntegration-v1"); HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); JsonFact
我使用以下代码更新现有的博客文章.我收到了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) 我也尝试了以下方法 方法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); 方法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("resourcesxxxx.p12")) .setServiceAccountScopes(collection) .build(); credential.setAccessToken("ya29.MQJqQi7HqTcJGoCCU-Lo5Ybdb1Otc-Z_fsAN97oySVsU84A7IXr_cPqWcrMe2raZoSvU"); service.setOAuth2Credentials(credential); 但我得到了同样的例外. 解决方法
看起来不支持Blog of Blog的API.我们必须使用V3 API.
下载后导入Jars:http://developers.google.com/blogger/docs/3.0/api-lib/java 然后按照这个答案:Using Java blogger API v3 to post on blog dynamically (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |