[英]My app can not create file in “appDataFolder” of google drive
How can I create a file in google drive "appDataFolder"?如何在谷歌驱动器“appDataFolder”中创建文件?
I created google developer console settings which is necessary for creating file and accessing "appDataFolder".我创建了创建文件和访问“appDataFolder”所必需的谷歌开发者控制台设置。
google developer console谷歌开发者控制台
Android Studio output error messages as below. 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)
Source code is源代码是
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();
If the following line is commented out in the source code, my app can create a file in google drive top folder not "appDataFolder".如果以下行在源代码中被注释掉,我的应用程序可以在谷歌驱动器顶部文件夹中创建一个文件,而不是“appDataFolder”。
body.setParents(Collections.singletonList("appDataFolder"));
Android Studio 4.1.2 enter image description here Android Studio 4.1.2在此处输入图像描述
Best regards.此致。 Thanks in advance.
提前致谢。
While from the looks of your developer console image you apear to have include a large number of scopes.从您的开发人员控制台图像的外观来看,您似乎包含大量范围。 The issue here is not with your settings in google developer console but rather with the scopes you have authorized your user with.
这里的问题不在于您在谷歌开发者控制台中的设置,而在于您已授权用户使用的范围。
When you run your application the user is asked to consent to your accessing some of their data, this is the scope of authorization for your application.当您运行您的应用程序时,用户被要求同意您访问他们的一些数据,这是您的应用程序的授权 scope。 It defines what the user has granted your application access to do.
它定义了用户授予您的应用程序访问权限的内容。
If your code only requests read only access then and you try to write to it then your going to see an insufficientScopes error message.如果您的代码只请求只读访问,然后您尝试写入它,那么您将看到一个不足的Scopes 错误消息。
You need to check what scope is required of the method you are using and then make sure when you authorize your user that you are requesting that scope.您需要检查您使用的方法需要什么 scope,然后确保在您授权用户时请求 scope。
My guess is you have something like this我的猜测是你有这样的东西
private static final List<String> SCOPES = Collections.singletonList(DriveScopes.DRIVE_METADATA_READONLY);
YOu need to include this scope https://www.googleapis.com/auth/drive.appdata
which is probably something like DriveScopes.DRIVE_APPDATA
您需要包含此 scope
https://www.googleapis.com/auth/drive.appdata
这可能类似于DriveScopes.DRIVE_APPDATA
Remember once the scopes have been changed in your application you will need to reauthorize the user.请记住,一旦在您的应用程序中更改了范围,您将需要重新授权用户。 If your storing the consent then it needs to be removed in order to request authorization again.
如果您存储同意,则需要将其删除才能再次请求授权。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.