使用 pydrive 从 OAUTH playground google 实现访问令牌

Implement access token from OAUTH playground google with pydrive

我尝试在使用 pydrive 的 google 驱动器上不请求权限的情况下进行身份验证,他们建议我遵循这个答案 answer on stack 但是当我获得访问令牌时,我真的不知道在哪里为了放置它,我尝试了 client_secret.json 但没有成功。

您关注的问题来自 2013,关注任何旧的问题都是废话。

首先使用Oauth2 playground仅用于测试和开发。如果您不使用自己的客户端 ID 和客户端密码,在 Oauth playground 上创建的令牌将很快过期。如果您使用自己的客户端 ID 和客户端密码,那么您的刷新令牌将在 7 天后过期。由于您不拥有该域,因此无法使用 Oauth 游乐场的重定向 uri 来验证应用程序。所有这些安全保护措施都是在 2013 年之前应用的。

假设您有一个单用户应用程序并且您将只访问您自己的驱动器帐户,那么您应该使用 service account

from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
from oauth2client.service_account import ServiceAccountCredentials

scope = ["https://www.googleapis.com/auth/drive"]
gauth = GoogleAuth()
gauth.auth_method = 'service'
gauth.credentials = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.json', scope)
drive = GoogleDrive(gauth)

about = drive.GetAbout()

print('Current user name:{}'.format(about['name']))
print('Root folder ID:{}'.format(about['rootFolderId']))
print('Total quota (bytes):{}'.format(about['quotaBytesTotal']))
print('Used quota (bytes):{}'.format(about['quotaBytesUsed']))


file_list = drive.ListFile().GetList()
for file1 in file_list:
  print('title: %s, id: %s' % (file1['title'], file1['id']))

你应该参考 pydrive github 页面 service account 或者我们官方 google api python 示例相反。