【问题标题】:OAuth2 service account credentials for Google Drive upload scriptGoogle Drive 上传脚本的 OAuth2 服务帐户凭据
【发布时间】:2015-10-27 11:50:09
【问题描述】:

如果之前有人问过这个问题,请提前道歉,但是我有一些关于我正在编写的服务器端 Python 脚本的一些基本问题,该脚本每晚将 CSV 文件上传到我们 Google Drive 帐户中的文件夹。文件夹所有者创建了一个 Google API 项目,为此启用了 Drive API 并创建了一个我已下载为 JSON 文件的凭据对象。该文件夹已与服务帐户电子邮件共享,因此我假设脚本现在可以访问该文件夹,一旦它被授权。

JSON 文件包含以下字段:private_key_idprivate_keyclient_emailclient_idauth_uritoken_uriauth_provider_x509_cert_urlclient_x509_cert_url

  1. 我猜我的脚本不需要所有这些 - 哪些是 OAuth2 授权的基本或强制字段?

  2. 此处给出的示例 Python 脚本

https://developers.google.com/drive/web/quickstart/python

似乎假设凭据是直接从 JSON 文件中检索的:

...
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
    os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
                               'drive-python-quickstart.json')

store = oauth2client.file.Storage(credential_path)
credentials = store.get()
...

但在我们的设置中,我们将它们存储在我们自己的数据库中,脚本通过字典访问它们。如果凭据在字典中,如何进行授权?

提前致谢。

【问题讨论】:

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


【解决方案1】:

浏览源代码后,似乎它被设计为只接受 JSON 文件。他们使用simplejson 进行编码和解码。看看源代码:

This 是从您提供的文件中获取内容的位置:

 62 def locked_get(self): 
 63      """Retrieve Credential from file. 
 64   
 65      Returns: 
 66        oauth2client.client.Credentials 
 67   
 68      Raises: 
 69        CredentialsFileSymbolicLinkError if the file is a symbolic link. 
 70      """ 
 71      credentials = None 
 72      self._validate_file() 
 73      try: 
 74        f = open(self._filename, 'rb') 
 75        content = f.read() 
 76        f.close() 
 77      except IOError: 
 78        return credentials 
 79   
 80      try: 
 81        credentials = Credentials.new_from_json(content) #<----!!!
 82        credentials.set_store(self) 
 83      except ValueError: 
 84        pass 
 85   
 86      return credentials 

new_from_json 中,他们尝试使用 simplejson 解码内容。

 205 def new_from_json(cls, s): 
 206      """Utility class method to instantiate a Credentials subclass from a JSON 
 207      representation produced by to_json(). 
 208   
 209      Args: 
 210        s: string, JSON from to_json(). 
 211   
 212      Returns: 
 213        An instance of the subclass of Credentials that was serialized with 
 214        to_json(). 
 215      """ 
 216      data = simplejson.loads(s) 

长话短说,您似乎必须从您的 dict 构造一个 JSON 文件。见this。基本上,您需要json.dumps(your_dictionary) 并创建一个文件。

【讨论】:

    【解决方案2】:

    实际上,SignedJwtAssertionCredentials 可能是一个答案。另一个可能是

    from oauth2client import service_account
    ...
    credentials = service_account._ServiceAccountCredentials(
          service_account_id=client.clid,
          service_account_email=client.email,
          private_key_id=client.private_key_id,
          private_key_pkcs8_text=client.private_key,
          scopes=client.auth_scopes,
    )
    ....
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2018-03-09
    • 1970-01-01
    • 2016-03-03
    • 2014-02-20
    • 2014-09-22
    • 2019-10-27
    相关资源
    最近更新 更多