【问题标题】:Implement access token from OAUTH playground google with pydrive使用 pydrive 从 OAUTH 游乐场谷歌实现访问令牌
【发布时间】:2022-01-20 07:25:57
【问题描述】:

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

【问题讨论】:

    标签: python google-api google-drive-api google-oauth pydrive


    【解决方案1】:

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

    首先,使用Oauth2 playground 仅用于测试和开发。如果您不使用自己的客户端 ID 和客户端密码,在 Oauth 游乐场创建的令牌将很快过期。如果您使用自己的客户端 ID 和客户端密码,那么您的刷新令牌将在 7 天内过期。由于您不拥有该域,因此无法使用 Oauth Playground 的重定向 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 sample

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-20
      • 2012-11-13
      • 1970-01-01
      • 2013-07-20
      • 2021-04-03
      • 2013-07-21
      • 2014-10-17
      • 2019-07-07
      相关资源
      最近更新 更多