本文介绍了com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正尝试将文本文件上传到我的Google云端硬盘帐户。无论如何,我总是遇到 com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden Stacktrace: 线程main中的异常com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden {code:403,errors:[{domain:global,message:Forbidden,原因:禁止}],消息:禁止} ,位于com.google.api.client.googleapis.json。 GoogleJsonResponseException.from(GoogleJsonResponseException.java:145),位于com.google.api处,位于com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)处。 client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40),位于com.google.api.client.googleapis.services.AbstractGoogleClientReque通过com.google.api.client获取st.executeUnparsed(AbstractGoogleClientRequest.java:423)的com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)。 googleabis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) at chatterjee.sandeep.javabase.miscellaneous.DriveCommandLine.main(DriveCommandLine.java:69) 以下是 DriveCommandLine.java File file = service.files()。insert(body,mediaContent).execute(); 完整代码: import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 导入com.google.api.client.googleapis.auth.oauth2.GoogleCredential; 导入com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; import com.google.api.client.http.FileContent; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; 导入com.google.api.services.drive.Drive; 导入com.google.api.services.drive.DriveScopes; 导入com.google.api.services.drive.model.File; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; public class DriveCommandLine { private static String CLIENT_ID =CLIENT_ID; private static String CLIENT_SECRET =CLIENT_SECRET; private static String REDIRECT_URI =REDIRECT_URI; public static void main(String [] args)throws IOException { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport,jsonFactory,CLIENT_ID,CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE_FILE))。setAccessType(online) .setApprovalPrompt(auto)。build(); String url = flow.newAuthorizationUrl()。setRedirectUri(REDIRECT_URI) .build(); System.out .println(请在浏览器中打开以下URL,然后输入授权码:); 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(); GoogleCredential凭证=新GoogleCredential() .setFromTokenResponse(回复); //创建一个新的授权API客户端 Drive service = new Drive.Builder(httpTransport,jsonFactory,凭证).build(); //插入一个文件 File body = new File(); body.setTitle(我的文档); body.setDescription(测试文档); body.setMimeType(text / plain); java.io.File fileContent = new java.io.File(/path/to/TextFile.txt); FileContent mediaContent = new FileContent(text / plain,fileContent); File file = service.files()。insert(body,mediaContent).execute(); System.out.println(文件ID:+ file.getId()); $ b我在我的项目构建路径中有以下jar包: commons-logging-1.1.1.jar google-api-client-1.18.0-rc.jar google-api-client-android-1.18.0-rc.jar google-api-client-appengine-1.18.0-rc.jar google-api-client-gson- 1.18.0-rc.jar google-api-client-jackson2-1.18.0-rc.jar google-api-client-java6-1.18.0-rc.jar google -api-client-servlet-1.18.0-rc.jar google-api-services-drive-v1-rev123-1.18.0-rc.jar google-http-client-1.18.0 -rc.jar google-http-client-android-1.18.0-rc.jar google-http-client-appengine-1.18.0-rc.jar google-http- client-gson-1.18.0-rc.jar google-http-client-jackson2-1.18.0-rc.jar google -http-client-jdo-1.18.0-rc.jar google-oauth-client-1.18.0-rc.jar google-oauth-client-appengine-1.18.0-rc.jar google-oauth-client-java6-1.18.0 -rc.jar google-oauth-client-jetty-1.18.0-rc.jar google-oauth-client-servlet-1.18.0 -r c.jar gson-2.1.jar httpclient-4.0.3.jar httpcore-4.0.1_1.jar jackson-core-2.1.3.jar jdo2-api-2.3-eb.jar jetty-util-6.1.26.jar jsr305-1.3.9.jar transaction-api-1.1-rev-1.jar 目前我有两个使用Drive API启动的项目。 现在我可以在哪里正确设置权限来解决这个问题? 另外,我在这里做错了什么? 有同样的问题,我做了以下更改,并解决了问题 $ b 将SheetsScopes.DRIVE添加到authorize()中给出的作用域中 private static final List< String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE); 2)创建一个新目录,以便下次运行它时,它将进行身份验证并保存新创建的目录的凭证 private static final java.io.File DATA_STORE_DIR = new java.io.File( System.getProperty(user.home),.credentials / 2 / sheets.googleapis.com-java-quickstart.json); I am trying to upload a text file to my Google Drive account. No matter what, I always encounter an com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 ForbiddenStacktrace:Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden{ "code" : 403, "errors" : [ { "domain" : "global", "message" : "Forbidden", "reason" : "forbidden" } ], "message" : "Forbidden"} at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:423) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) at chatterjee.sandeep.javabase.miscellaneous.DriveCommandLine.main(DriveCommandLine.java:69)Here is line 69 of DriveCommandLine.javaFile file = service.files().insert(body, mediaContent).execute();Complete Code:import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;import com.google.api.client.http.FileContent;import com.google.api.client.http.HttpTransport;import com.google.api.client.http.javanet.NetHttpTransport;import com.google.api.client.json.JsonFactory;import com.google.api.client.json.jackson2.JacksonFactory;import com.google.api.services.drive.Drive;import com.google.api.services.drive.DriveScopes;import com.google.api.services.drive.model.File;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Arrays;public class DriveCommandLine { private static String CLIENT_ID = "CLIENT_ID"; private static String CLIENT_SECRET = "CLIENT_SECRET"; private static String REDIRECT_URI = "REDIRECT_URI"; public static void main(String[] args) throws IOException { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE_FILE)).setAccessType("online") .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(); GoogleCredential credential = new GoogleCredential() .setFromTokenResponse(response); // Create a new authorized API client Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).build(); // Insert a file File body = new File(); body.setTitle("My document"); body.setDescription("A test document"); body.setMimeType("text/plain"); java.io.File fileContent = new java.io.File( "/path/to/TextFile.txt"); FileContent mediaContent = new FileContent("text/plain", fileContent); File file = service.files().insert(body, mediaContent).execute(); System.out.println("File ID: " + file.getId()); }}I have the following jars in my project build path:commons-logging-1.1.1.jargoogle-api-client-1.18.0-rc.jargoogle-api-client-android-1.18.0-rc.jargoogle-api-client-appengine-1.18.0-rc.jargoogle-api-client-gson-1.18.0-rc.jargoogle-api-client-jackson2-1.18.0-rc.jargoogle-api-client-java6-1.18.0-rc.jargoogle-api-client-servlet-1.18.0-rc.jargoogle-api-services-drive-v1-rev123-1.18.0-rc.jargoogle-http-client-1.18.0-rc.jargoogle-http-client-android-1.18.0-rc.jargoogle-http-client-appengine-1.18.0-rc.jargoogle-http-client-gson-1.18.0-rc.jargoogle-http-client-jackson2-1.18.0-rc.jargoogle-http-client-jdo-1.18.0-rc.jargoogle-oauth-client-1.18.0-rc.jargoogle-oauth-client-appengine-1.18.0-rc.jargoogle-oauth-client-java6-1.18.0-rc.jargoogle-oauth-client-jetty-1.18.0-rc.jargoogle-oauth-client-servlet-1.18.0-rc.jargson-2.1.jarhttpclient-4.0.3.jarhttpcore-4.0.1_1.jarjackson-core-2.1.3.jarjdo2-api-2.3-eb.jarjetty-util-6.1.26.jarjsr305-1.3.9.jartransaction-api-1.1-rev-1.jarAt present I have two projects set up with Drive API enabled.Now where do I properly set up the permissions to resolve this issue?Also, what am I doing wrong here? 解决方案 I had the same issue, I did the following change and it resolved the issue1) Added SheetsScopes.DRIVE to the scopes to be given in authorize()private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS,SheetsScopes.DRIVE);2) Created a new directory, so that next time I run it, it will authenticate and save the credential to the newly created directoryprivate static final java.io.File DATA_STORE_DIR = new java.io.File( System.getProperty("user.home"), ".credentials/2/sheets.googleapis.com-java-quickstart.json"); 这篇关于com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 22:58