简体   繁体   English

OAuth API Google Drive Python

[英]OAuth API Google Drive Python

I am trying to write a Python program that makes a file in Google Drive.我正在尝试编写一个在 Google Drive 中创建文件的 Python 程序。 But if I try to do it, it says但如果我尝试这样做,它会说

'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
()'

Then it opens the browser and I get the following:然后它打开浏览器,我得到以下信息:

"
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."

Does Google have a newer version?谷歌有更新的版本吗? Or what's the problem?或者有什么问题?

My code is:我的代码是:

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']))

Based from Handling API Errors , an error code 401 indicates invalid credentials.根据处理 API 错误错误代码 401表示凭据无效。 This error is usually encountered due to invalid authorization header or the access token you're using is either expired or invalid.由于授权标头无效或您使用的访问令牌已过期或无效,通常会遇到此错误。

Suggested action for such error is to refresh the access token using the long-lived refresh token.针对此类错误的建议操作是使用长期存在的刷新令牌刷新访问令牌。 If this fails, direct the user through the OAuth flow, as described in Authorizing Your App with Google Drive .如果此操作失败,请引导用户完成 OAuth 流程,如使用 Google Drive 授权您的应用程序中所述。

Furthermore, "The OAuth client was deleted" error message was also displayed.此外,还显示“OAuth 客户端已删除”错误消息。 You may want to check if there's an existing client ID.您可能需要检查是否存在现有的客户端 ID。 If none, you need to obtain new OAuth 2.0 credentials from the Google API Console.如果没有,您需要从 Google API 控制台获取新的 OAuth 2.0 凭据。 Here are the basic steps in Using OAuth 2.0 to Access Google APIs :以下是使用 OAuth 2.0 访问 Google API的基本步骤:

  1. Obtain OAuth 2.0 credentials from the Google API Console .Google API Console获取 OAuth 2.0 凭据。
  2. Obtain an access token from the Google Authorization Server.从 Google 授权服务器获取访问令牌。
  3. Send the access token to an API.将访问令牌发送到 API。
  4. Refresh the access token, if necessary.如有必要,刷新访问令牌。

Lastly, you may want to also check the best practices in Handling errors: revoked or invalid tokens and also Token expiration so you can better anticipate the possibility that a granted token might no longer work.最后,您可能还想查看处理错误中的最佳实践:已撤销或无效的令牌以及令牌过期,以便您可以更好地预测授予的令牌可能不再起作用的可能性。

Hope that helps!希望有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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