当前位置: 首页 > 知识库问答 >
问题:

在google drive rest api java中插入文件时出现权限异常

陶寒
2023-03-14

我正在尝试上传文件到谷歌驱动器使用驱动器rest api,遵循本教程。

对于授权,功能为:

 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 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 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.java:469)在com.teamchat.google.sheet.integration.drivequickstart.insertfile(drivequickstart.java:137)位于com.teamchat.google.sheet.integration.drivequickstart.main(drivequickstart.java:110)

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

所以我能够得到文件列表,但不能插入文件在谷歌驱动器。谁能告诉我我做错了什么。

共有1个答案

胡锋
2023-03-14

“原因”:“InsulficentPermissions”

这意味着您在获得授权时设置的范围不允许您对资源进行写入。

设置正确的作用域https://developers.google.com/drive/web/scopes或https://developers.google.com/resources/api-libraries/documentation/drive/v2/java/last/com/google/api/services/drive/drivecopes.html

通常:

private static final List<String> SCOPES =
        Arrays.asList(DriveScopes.DRIVE_FILE);
 类似资料:
  • 我试图在txt文件中写入从html表单获取的数据,但出现以下错误:警告:fopen(/var/www/4/datos.txt):无法打开流:在/var/www/4/comprobacionformulaio中的权限被拒绝。php第13行警告:fwrite()希望参数1是资源,布尔值在/var/www/4/comprobacionformulaio中给出。php第16行警告:fclose()希望参数

  • e 单列的数据类型从 NUMBER(4) 更改为 VARCHAR2(4)。DBA对现有值进行了转换。 现在,当我们尝试使用proc*c代码将记录插入表中时,会出现错误“ORA-01031:权限不足”。 从SQLPLUS我们能够插入记录。一段时间后问题消失了。一旦DBA重建表,问题就消失了。 每次我们用新的更改刷新测试环境后,都会出现此问题。

  • 我在elasticsearch插件中遇到了异常: java.security.AccessControlExcture:访问拒绝(org.elasticsearch.ThreadPersionorg.elasticsearch.ThreadPersion)java.security.AccessControlContext.check权限(AccessControlContext.java:472

  • 尝试在中显示文件时遇到问题。异常如下所示:。 代码如下: 异常消息为: 线程“main”com.JNIWrapper.libraryNotFoundException:在java.library.path中找不到JNIWrapper本机库(jniwrap.dll):c:\program files\java\jdk1.7.0_05\bin;c:\windows\sun\java\bin;c:\wi

  • 问题内容: 在查询Android日历中的事件之前,我需要检查权限。为此,Android studio警告我在查询之前需要先进行检查。自动生成的代码如下: 尝试运行它时,出现以下错误: 尝试 在空对象引用上调用虚拟方法’int android.content.Context.checkPermission(java.lang.String,int,int)’ 因此很明显,此时某些东西为空,我试图以不

  • IntelliJ IDEA 2017.3.4(Community Edition)Build#ic-173.4548.28,2018年1月30日构建jre:1.8.0_152-release-1024-b11 x86_64 jvm:OpenJDK 64位服务器VM,由JetBrains S.r.o Mac OS X 10.13.3开发 代号一插件3.8.3 错误消息: