Java “我的应用程序无法在”中创建文件;appDataFolder";谷歌硬盘

Java “我的应用程序无法在”中创建文件;appDataFolder";谷歌硬盘,java,android-studio,google-drive-api,google-oauth,google-api-java-client,Java,Android Studio,Google Drive Api,Google Oauth,Google Api Java Client,如何在google drive“appDataFolder”中创建文件 我创建了谷歌开发者控制台设置,这是创建文件和访问“appDataFolder”所必需的 Android Studio输出错误消息如下 W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden { "code": 403, "erro

如何在google drive“appDataFolder”中创建文件

我创建了谷歌开发者控制台设置,这是创建文件和访问“appDataFolder”所必需的

Android Studio输出错误消息如下

W/System.err: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
    {
      "code": 403,
      "errors": [
        {
          "domain": "global",
          "message": "The granted scopes do not allow use of the Application Data folder.",
          "reason": "insufficientScopes"
        }
      ],
      "message": "The granted scopes do not allow use of the Application Data folder."
    }
W/System.err:     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:432)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
        at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
        at com.example.test1.MainActivity$3.run(MainActivity.java:131)
        at java.lang.Thread.run(Thread.java:923)
源代码是

                        File body = new File();
                        body.setName(FILE_TITLE);//fileContent.getName());
                        body.setMimeType("text/plain");
                        body.setParents(Collections.singletonList("appDataFolder"));

                        File file = service.files().create(body, content)
                                .setFields("id")
                                .execute();

如果在源代码中注释掉以下行,我的应用程序可以在google drive中创建一个文件 顶部文件夹不是“appDataFolder”

Android Studio 4.1.2

致以最良好的祝愿。
提前感谢。

从开发人员控制台映像的外观来看,您可以看到包含了大量作用域。这里的问题不是您在google开发者控制台中的设置,而是您授权用户使用的范围

运行应用程序时,会要求用户同意您访问其部分数据,这是应用程序的授权范围。它定义了用户授予应用程序访问权限的操作

如果您的代码只请求只读访问,然后您尝试写入它,那么您将看到一条insufficientScopes错误消息

您需要检查所使用的方法需要什么作用域,然后在授权用户时确保您正在请求该作用域

我猜你有类似的东西

private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
private static final List SCOPES=Collections.singletonList(DriveScopes.DRIVE\u METADATA\u READONLY);
您需要包括此范围
https://www.googleapis.com/auth/drive.appdata
可能类似于
DriveScopes.DRIVE\u APPDATA


请记住,一旦应用程序中的作用域发生更改,您就需要重新授权用户。如果您正在存储许可,则需要将其删除,以便再次请求授权。

感谢您的快速评论。请问您为什么认为我的代码只要求只读访问?我希望我的应用程序尝试写入(创建文件)。在我的android应用程序屏幕布局中有一个EditText和两个按钮。按下“保存”按钮后,如您所说,我的android终端上会出现OAuth同意屏幕。同意屏幕要求我显示、编辑、创建和删除google drive上的所有文件。在我同意后,我的应用程序请求在名为“appDataFolder”的特定文件夹中创建一个文件,该文件对google驱动器数据的所有者不可见。但是,尽管包含“../auth/drive.appdata”,仍出现了错误范围进入谷歌开发者控制台的同意设置屏幕。我猜你只是请求只读访问,因为你没有包括授权码,我们可以看到你请求的范围。一旦更改了作用域,请记住,您需要强制用户重新授权以使其接受更改,否则您的代码将运行旧的approve.credential=GoogleAccountCredential.UsingAuth2(this,Arrays.asList(DriveScopes.DRIVE,DriveScopes.DRIVE_APPDATA));
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);