0

I want to get drive file of user (metadata, content, downloadUrl) not created by my application with access token have only drive.file scope. Can anyone have a solution for this one? Thank you.

 gapi.client.load('drive', 'v3', () => {
  gapi.auth.setToken({
    access_token: user.services.google.accessToken,
    expires_in: user.services.google.expiresAt,
  });
  gapi.client.drive.files.get({
    fileId: file.id,
    fields: 'webContentLink',
  }).then( test => {
    console.log('test', test);
  })
});
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • You want to get a drive file which is on your drive, created / owned by you? If not, please explain in more detail. – ziganotschka Dec 11 '19 at 11:00
  • Dear @ziganotschka, I have an access token with scope drive.file after user login into my app. Now, I want to get file from drive store of user but drive.files is limited. Can you have solution for this without request more permission? – Tran Minh Nhut Dec 11 '19 at 11:08
  • I am not sure if I understand your issue correctly, but `drive.file` only gives you `Per-file access to files created or opened by the app`, see here: https://developers.google.com/drive/api/v2/about-auth – ziganotschka Dec 11 '19 at 11:16
  • I understand that scope, so I need to find solution to access other file which is not created by my app without request more permission because my client doesn't want request scope restricted. – Tran Minh Nhut Dec 11 '19 at 11:21
  • In this case, please explain your situation better. Are you using a simple script, a Web App or an Add-on? Is the client trying to access his file on his drive or are you trying to access the client's file (on your drive or on client's drive?)? How is the file created? Can you show a code snippet? – ziganotschka Dec 11 '19 at 11:25
  • In my Web app, user call picker and pick a file from drive, after that I get fileID of that file. Then, I send a request to get data of that file (ex: webContentLink) with fileID. Please see my code above. – Tran Minh Nhut Dec 11 '19 at 11:51

1 Answers1

0

You need to have access to user's file in order to obtain the webContentLink

You can incorporate in your script a function that allows the user to share his file with you, something like:

drive.permissions.create({
    resource: {
        'type': 'user',
        'role': 'reader',
        'emailAddress': 'YOUR EMAIL'
    }, 
        fileId: file.id,
    }
...);
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thanks for your reply, but it's not resolve my problem. If I call drive.permissions.create it's throw an error `File not found` because access token with only drive.file scope I can't access to private file of user. I need a solution like make a request to get specific private file of user. – Tran Minh Nhut Dec 11 '19 at 14:27
  • It is not you, it is the user who needs to give you permission for his file at the moment when he uploads the file. You can implement it e.g. by creating an Apps Script Web App. When the user choses his file, you can request him to press on a submit button, which will redirect him to the URL of the WebApp where he gives the permission to share the chosen file with you. – ziganotschka Dec 11 '19 at 15:13
  • I understand, It's mean everytime user pick a private file I will request user to share file with me, right ? Can you explain for detail which API or how to implementation request for share document ? I don't see anything like that through google docs. – Tran Minh Nhut Dec 11 '19 at 15:24
  • Are you familiar with Apps Script and Apps Script Web App? https://developers.google.com/apps-script/guides/web The Web App allows you to make your script accessible to anyone to whom you provide the URL. It gives you the option that the user clicking on the link (or being redirected through the browser) runs the script as himself and thus has the authorization to share his file with you. – ziganotschka Dec 11 '19 at 15:34
  • 1
    I will try it. Thank you. Best Regards. – Tran Minh Nhut Dec 11 '19 at 15:55
  • Dear @ziganotschka I try it but it’s still need user grant permission drive or drive.readonly scope for app script. Did you have any solution that only request user share file picked and not request more permission to access – Tran Minh Nhut Dec 12 '19 at 03:15
  • I am afraid that at some stage the user does need to give you more permission. The only workaround is to ask the user to manually share with you the file he picked. – ziganotschka Dec 12 '19 at 08:36