简体   繁体   中英

Google Drive API : Can't delete file

I want to use Google Drive API to manage files programaticaly. To do that, I use ID clients OAuth 2.0 Web Application . In OAuth consent screen (Edit App) , I check .../auth/drive and .../auth/drive.readonly scopes.

I'm log in, I have token.json file. I can list and download files

Now, when I want to delete file, I do this in python: service.files().delete(fileId=item['id']).execute() . But I have this issue:

An error occurred: <HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/<fileID>? returned "The user has not granted the app <appID> write access to the file <fileID>.". Details: "[{'message': 'The user has not granted the app <appID> write access to the file <fileID>.', 'domain': 'global', 'reason': 'appNotAuthorizedToFile', 'location': 'Authorization', 'locationType': 'header'}]">

What I do wrong?

Thanks in advance !

The scopes declared in Google cloud console are mainly for use with verification of your application. Its your code that defines what scopes your application is requesting of the user.

I am going to assume that you are following the Quick start for python or quick start for node.js both use token.json for credential storage. The answer is the same either way.

This sample shows you how to use thefiles.list method which allows for a readonly scope.

在此处输入图像描述

While the file.delete does not allow for a readonly scope it requires write access.

在此处输入图像描述

Fix

In the code you can see that the scope is readonly

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']

This means that when the user authorized your app they authorized it with a readonly scope giving you access to read the files only. This the users credentials are now stored in token.json

To fix your issue change the scope to

SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly']

Then delete the token.json file and your app should now be resting full drive access giving you the ability to make changes like delete the files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM