如何在Google驱动器“ appDataFolder”中创建文件?
我创建了Google开发者控制台设置,这对于创建文件和访问“ 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驱动器的顶层文件夹中创建文件,而不是“ appDataFolder”。
body.setParents(Collections.singletonList("appDataFolder"));
Android Studio 4.1.2在此处输入图片描述
最好的祝福。提前致谢。
从开发人员控制台映像的外观看,您似乎包含了很多范围。这里的问题不在于您在Google Developer Console中的设置,而在于您授权用户使用的范围。
当您运行应用程序时,系统会要求用户同意您访问其某些数据,这是您的应用程序的授权范围。它定义了用户已授予您的应用程序访问权限的操作。
如果您的代码仅请求只读访问权限,然后您尝试对其进行写入,那么您将看到不足的Scopes错误消息。
您需要检查所使用的方法需要什么范围,然后确保在授权用户时请求了该范围。
我的猜测是你有这样的事情
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
您需要包括此范围https://www.googleapis.com/auth/drive.appdata
,可能类似于DriveScopes.DRIVE_APPDATA
请记住,一旦您的应用程序中的范围已更改,您将需要重新授权用户。如果您存储同意书,则需要将其删除以再次请求授权。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句