![](/img/trans.png)
[英]How do I create & edit a spreadsheet file on google drive using Google Drive APIs only from my Android app?
[英]My app can not create file in “appDataFolder” of google drive
如何在谷歌驱动器“appDataFolder”中创建文件?
我创建了创建文件和访问“appDataFolder”所必需的谷歌开发者控制台设置。
Android Studio output 错误信息如下。
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();
如果以下行在源代码中被注释掉,我的应用程序可以在谷歌驱动器顶部文件夹中创建一个文件,而不是“appDataFolder”。
body.setParents(Collections.singletonList("appDataFolder"));
Android Studio 4.1.2在此处输入图像描述
此致。 提前致谢。
从您的开发人员控制台图像的外观来看,您似乎包含大量范围。 这里的问题不在于您在谷歌开发者控制台中的设置,而在于您已授权用户使用的范围。
当您运行您的应用程序时,用户被要求同意您访问他们的一些数据,这是您的应用程序的授权 scope。 它定义了用户授予您的应用程序访问权限的内容。
如果您的代码只请求只读访问,然后您尝试写入它,那么您将看到一个不足的Scopes 错误消息。
您需要检查您使用的方法需要什么 scope,然后确保在您授权用户时请求 scope。
我的猜测是你有这样的东西
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
您需要包含此 scope https://www.googleapis.com/auth/drive.appdata
这可能类似于DriveScopes.DRIVE_APPDATA
请记住,一旦在您的应用程序中更改了范围,您将需要重新授权用户。 如果您存储同意,则需要将其删除才能再次请求授权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.