首页 > 解决方案 > Google Drive API:用户未授予应用程序错误

问题描述

我正在关注Quickstarthttps://developers.google.com/drive/api/v3/quickstart/python 上内容。我已经通过页面启用了驱动器 API,加载了credentials.json并且可以成功列出我的谷歌驱动器中的文件。但是,当我想下载文件时,我收到了消息

`The user has not granted the app ####### read access to the file`

除了将该范围放入我的代码之外,我还需要做更多的事情还是需要激活其他东西?

SCOPES = 'https://www.googleapis.com/auth/drive.file'
client.flow_from_clientsecrets('credentials.json', SCOPES)

标签: pythongoogle-drive-apigoogle-oauthgoogle-api-python-client

解决方案


一旦你经历了Quick-Start Tutorial最初的范围,范围如下:

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

因此,在列出文件并且您决定下载之后,它将无法正常工作,因为您需要再次生成令牌,因此更改范围不会重新创建或提示您进行首次运行时发生的“谷歌授权”。

要强制生成令牌,只需删除您的当前令牌或使用新文件从存储中存储您的密钥:

store = file.Storage('tokenWrite.json')

推荐阅读