2 java google-api-java-client google-drive-api google-oauth android-studio
如何在谷歌驱动器“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)
Run Code Online (Sandbox Code Playgroud)
源代码是
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();
Run Code Online (Sandbox Code Playgroud)
如果在源代码中注释掉以下行,我的应用程序可以在谷歌驱动器顶部文件夹而不是“appDataFolder”中创建文件。
body.setParents(Collections.singletonList("appDataFolder"));
Run Code Online (Sandbox Code Playgroud)
Android Studio 4.1.2 在此处输入图像描述
此致。提前致谢。
从开发者控制台图像的外观来看,您似乎包含大量范围。这里的问题不在于您在谷歌开发者控制台中的设置,而在于您授权用户的范围。
当您运行应用程序时,系统会要求用户同意您访问他们的某些数据,这是您的应用程序的授权范围。它定义用户授予您的应用程序访问权限以执行的操作。
如果您的代码仅请求只读访问权限,并且您尝试对其进行写入,那么您将看到不够范围错误消息。
您需要检查您正在使用的方法需要什么范围,然后确保当您授权用户时您正在请求该范围。
我猜你有这样的东西
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
Run Code Online (Sandbox Code Playgroud)
您需要包括这个范围,https://www.googleapis.com/auth/drive.appdata可能类似于DriveScopes.DRIVE_APPDATA
请记住,一旦应用程序中的范围发生更改,您将需要重新授权用户。如果您存储同意,则需要将其删除才能再次请求授权。
| 归档时间: |
|
| 查看次数: |
768 次 |
| 最近记录: |