com.google.api.client.googleapis.json.GoogleJsonResponseException:403 Forbidden

时间:2015-09-09 10:10:44

标签: java google-drive-api

我正在尝试将文本文件上传到我的Google云端硬盘帐户。无论如何,我总是遇到com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden

堆栈跟踪:

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)

以下是DriveCommandLine.java

的第69行
File file = service.files().insert(body, mediaContent).execute();

完整代码:

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());
    }
}

我的项目构建路径中有以下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-rc.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的项目。

enter image description here

现在我在哪里正确设置解决此问题的权限?

另外,我在这里做错了什么?

8 个答案:

答案 0 :(得分:6)

我遇到了同样的问题,我做了以下更改并解决了问题

1)将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");

答案 1 :(得分:3)

经过长时间的搜索,我发现这个答案隐藏在搜索引擎中:

将定义范围的行更改为:

private static final List<String> SCOPES = Arrays.asList(GmailScopes.MAIL_GOOGLE_COM);

https://stackoverflow.com/a/38599382/1317559

答案 2 :(得分:2)

我刚遇到此问题,您可能已使用不允许插入文件的作用域创建了凭据文件。删除凭据文件并重新运行。

答案 3 :(得分:2)

当您更改SCOPE并继续使用旧令牌时,就会发生此问题。只需删除StoredCredential文件即可。Idea

答案 4 :(得分:1)

几个月来一直有相同的配额错误。尝试了一切。它看起来像谷歌方面的问题。 在所有Google产品中,Calendar API无法支持他们的团队。如果有人知道或已经能够与Google团队联系有关Calendar API的信息,请告诉我们!

答案 5 :(得分:1)

对我来说问题是SCOPES,它被设置为SheetsScopes.SPREADSHEETS_READONLY

所以我把它改成了SheetsScopes.SPREADSHEETS,把项目目录下的Token文件夹删掉,再次运行程序就成功了。

答案 6 :(得分:0)

对我来说问题是网址是http ...而且它应该是https ...

答案 7 :(得分:0)

我有同样的问题但在我的情况下解决这个问题:

private static final List<String> SCOPES =
        Arrays.asList(DriveScopes.DRIVE);

来自DriveScopes

并创建一个新目录

private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/2/drive-java-quickstart.json");

我解决了问题