OAuth API Google Drive Python
问题描述:
我正在尝试编写一个在Google Drive中制作文件的Python程序。但是,如果我尝试这样做,它说OAuth API Google Drive Python
'Your browser has been opened to visit:
()
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&response_type=code&client_id=742339009631-b7rv2mnplb22rhdjb1m663aken700cfe.apps.googleusercontent.com&access_type=offline
()'
然后打开浏览器,我得到如下:
"
401. That’s an error.
Error: deleted_client
The OAuth client was deleted.
Request Details
scope=https://www.googleapis.com/auth/drive
redirect_uri=http://localhost:8080/
response_type=code
client_id=742339009631-b7rv2mnplb22rhdjb1m663aken700cfe.apps.googleusercontent.com
access_type=offline
That’s all we know."
谷歌是否有更新的版本?或者是什么问题?
我的代码是:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
drive = GoogleDrive(gauth)
f = drive.CreateFile()
f.SetContentFile('hello.txt')
f.Upload()
print('title: %s, mimeType: %s' % (f['title'], f['mimeType']))
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
答
从Handling API Errors基础,一个error code 401表示凭据无效。由于无效授权标头或您使用的访问令牌已过期或无效,通常会遇到此错误。
建议此类错误的操作是使用长期刷新令牌刷新访问令牌。如果失败,请指导用户通过OAuth流程,如Authorizing Your App with Google Drive中所述。
此外,还显示“OAuth客户端已删除”错误消息。您可能需要检查是否有现有的客户端ID。如果没有,您需要从Google API控制台获取新的OAuth 2.0凭据。以下是基本步骤Using OAuth 2.0 to Access Google APIs:
- 获取的OAuth从Google API Console 2.0凭据。
- 从Google授权服务器获取访问令牌。
- 将访问令牌发送到API。
- 如有必要,刷新访问令牌。
最后,您可能还需要检查在Handling errors: revoked or invalid tokens的最佳实践,也Token expiration这样你就可以更好地预测这种可能性,即授权令牌可能不再起作用。
希望有帮助!