【问题标题】:Permission exception while inserting file in google drive rest api java在google drive rest api java中插入文件时出现权限异常
【发布时间】:2016-01-09 21:26:50
【问题描述】:

我正在尝试使用 drive rest api 将文件上传到谷歌驱动器,遵循此tutorial

对于授权,函数是:

 public static Credential authorize() throws IOException {
    // Load client secrets.
    InputStream in =
        DriveQuickstart.class.getResourceAsStream("client_secret.json");
    GoogleClientSecrets clientSecrets =
        GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(DATA_STORE_FACTORY)
            .setAccessType("offline")
            .build();
    Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver()).authorize("user");
    System.out.println(
            "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
    return credential;
}

我获取驱动器中所有文件的功能工作正常。

private static void listFiles(Drive service) throws IOException
{
     // Print the names and IDs for up to 10 files.
    FileList result = service.files().list().setMaxResults(10).execute();
    List<File> files = result.getItems();
    if (files == null || files.size() == 0) {
        System.out.println("No files found.");
    } else {
        System.out.println("Files:");
        for (File file : files) {
            System.out.printf("%s (%s)\n", file.getTitle(), file.getId());
        }
    }
}

但是当我尝试在驱动器中创建和插入文件时出现异常,功能和异常如下:

private static void insertFile(Drive service, String title, String description) {
      // File's metadata.
      File file = new File();
      file.setTitle(title);
      file.setDescription(description);
      file.setMimeType("application/vnd.google-apps.drive-sdk");

      try {
        file = service.files().insert(file).execute();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


      // Print the new file ID.
      System.out.println("File ID: %s" + file.getId());
    }

例外是:

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

{
  "code" : 403,
  "errors" : [ {
  "domain" : "global",
  "message" : "Insufficient Permission",
  "reason" : "insufficientPermissions"
  } ],
 "message" : "Insufficient Permission"
}

文件 ID:%snull 在 com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) 在 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) 在 com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321) 在 com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1056) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 在 com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 在 com.teamchat.google.sheet.integration.DriveQuickstart.insertFile(DriveQuickstart.java:137) 在 com.teamchat.google.sheet.integration.DriveQuickstart.main(DriveQuickstart.java:110)

这只是一个简单的 java 项目并使用 OAuth。

所以我能够获取文件列表但无法在谷歌驱动器中插入文件。 谁能告诉我我做错了什么。

【问题讨论】:

    标签: java google-drive-api


    【解决方案1】:

    “原因”:“权限不足”

    这意味着你在获得授权时设置的范围不允许你在资源上写。

    设置正确的范围https://developers.google.com/drive/web/scopeshttps://developers.google.com/resources/api-libraries/documentation/drive/v2/java/latest/com/google/api/services/drive/DriveScopes.html

    通常:

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

    【讨论】:

    • 感谢您的回答,所以我应该在某处设置范围,如果是,那么在哪里?我读了很多关于选择范围的文章,但找不到任何关于在哪里设置或使用它的信息。
    • 你应该已经设置了范围列表,看看你的SCOPES属性。我认为您设置了只读访问权限,例如 DRIVE_METADATA_READONLYDRIVE_READONLY
    • 感谢您的帮助。
    【解决方案2】:

    问题在于作用域

    将此行改为

    private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
    

    private static final List<String> SCOPES = new ArrayList(DriveScopes.all());"
    

    或根据需要更改范围的值。阅读this

    记得删除令牌文件夹并重新运行应用程序。

    【讨论】:

      猜你喜欢
      • 2017-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 2021-05-20
      • 2012-07-13
      • 2013-07-18
      相关资源
      最近更新 更多