Drive API for Android unable to access the file cr

2020-02-07 06:38发布

I am writing an app where one user will write a file through this app and share it with other users using a link. Other users will read the file using same mobile app. I am using 'Drive API for Android' to use the offline writing and incremental updates feature so that readers will be able to download only that part of file which is newly added or updated instead of full file download every time.

But unfortunately the file/folder created by this app by one user is not accessible through the same app on other devices. I am using method Drive.DriveApi.fetchDriveId with the folder id and getting following error message.

"Drive item not found, or you are not authorized to access it."

File/folder is accessible on same device with same id, but not accessible on other device, which indicates that second part of error message is the real reason.

With the current scope (Drive.SCOPE_FILE), I was hoping that file will be accessible as it is created by the same app, but it is not working.

So my question is "does Google Drive store device information also while creating files/folders and doesn't allow the same app on different devices to use it?" If not, it must be my mistake in the implementation, which I can debug further.

3条回答
乱世女痞
2楼-- · 2020-02-07 07:14

You can add full permissive drive scope - https://www.googleapis.com/auth/drive

request scope using -

.requestScopes(new Scope("https://www.googleapis.com/auth/drive"))

instead of

.resuestScopes(Drive.SCOPE_FILE)
查看更多
▲ chillily
3楼-- · 2020-02-07 07:16

I had a similar issue but not with an android app but a JavaScript app (should not matter IMO) and I wanted to access a shared file not a folder. However, I guess my workaround could help you as well.

The first approach is using the full auth scope (see https://developers.google.com/drive/web/scopes)

https://www.googleapis.com/auth/drive

But that is not really desired since you should request as few rights as possible from your users. My work-around was therefore as follows:

  1. I used the Drive UI Integration
    • Goto https://console.developers.google.com/apis/api/drive/drive_sdk
    • select the "Drive UI Integration" tab
    • fill out the form, I have used the following:
      • "Application Name"
      • The mandatory "Application icons"
      • activated "Automatically show OAuth 2.0 consent screen when users open my application from Google Drive" and entered my "CLIENT ID"
      • used a dummy url for "Open Url" (e.g., http://example.com)
      • provided a default file extension (this way your app will be suggested afterwards when the user opens a file)
    • save your changes
  2. Add the following scope to your app

https://www.googleapis.com/auth/drive.install

  1. The users will install your app when approving the requested rights
  2. Request your users to open the shared files with your "dummy" app in the google drive folder, which will grant your app access to the file (as stated here: https://developers.google.com/drive/web/scopes -- https://www.googleapis.com/auth/drive.file = Per-file access to files created or opened by the app) -- notice that the user will see an error message when opening your dummy app. You should probably implement an open url with something saying "thank's for granting access to the file xYZ".
  3. your app is now able to access the shared file.

I know, it is not very user-friendly but it works (at least now, might be that this is not intentional) If you know a way to automatically open a file with an app by invoking some URL, then please let me know. (yet, that would kind of be a security issue in the Google API, so I guess it is not possible).

Since it is somewhat cumbersome, I will propose the following two options to the user (so she/he can decide on her/his own):

  1. Give the app full access and don't be concerned about this issue at the cost of privacy (I am certainly not evil and would not do anything with the files, but might be that someone can hack my app and get access to the accounts and then... well, then the user would have wished to give my app less rights.)
  2. Give only install rights in addition but with the pain to open every single file which the app needs access to. In my case this is relatively rare so it should not really bother the user.

I hope that helps.

查看更多
时光不老,我们不散
4楼-- · 2020-02-07 07:24

Assuming this as a limitation of Drive API for Android, I am switching to Drive REST API. I found the functionality I was looking for in the Drive REST API i.e. incremental updates to the file and resumable downloads. Just that I need to manage some of the things on my own.

In that way, Drive REST API is complete and it can provide all the functionalities whereas Drive API for Android is constrained with lot of limitations. With no answers to my questions, I am assuming that it is not possible to share files with other users through Google Drive using Drive API for Android.

查看更多
登录 后发表回答