是否可以对gdata-python-client使用“域范围的授权”?

是否可以对gdata-python-client使用“域范围的授权”?,第1张

是否可以对gdata-python-client使用“域范围的授权”?

这是一个使用gdata库在Google App Engine中如何在Python中进行域范围委派的示例:

  1. 创建一个项目(https://cloud.google.com/console#/project)。

  2. 在“ API和验证”下,启用您需要使用的API(某些gdata API不会出现在此处,如果是,请跳过此步骤)。

  3. 在“ API和身份验证”->“凭据”下,创建一个新的OAuth2客户ID类型的服务帐户。记下电子邮件地址和客户端ID,然后将下载的私钥保存到安全位置。

  4. 作为域管理员,请转到管理控制台(https://admin.google.com/AdminHome),导航至“安全”->“高级设置”->“托管的第三方OAuth客户端访问权限”。

  5. 将先前的完整客户端ID粘贴到“客户端名称”字段中,并将API访问所需的范围粘贴到范围字段中。

  6. 由于我们在Google App Engine上运行,因此我们需要将PKCS12格式的私钥转换为PEM格式(因为当前在Google App Engine上部署的PyCrypto库不支持PCKS12):

    cat secret-privatekey.p12 | openssl pkcs12 -nodes -nocerts -passin pass:notasecret | openssl rsa > secret-privatekey.pem
  7. 将此文件放在您的应用目录中。

  8. 从https://pre.google.com/p/google-api-python-client/downloads/list下载Google API Python客户端,然后选择

    google-api-python-client-gae-1.2.zip

  9. 将其解压缩到您的应用目录中:

    unzip ~/Downloads/google-api-python-client-gae-1.2.zip
  10. 从https://pre.google.com/p/gdata-python-client/downloads/list下载gdata python客户端,选择

    gdata-2.0.18.zip

  11. 将其安装在您的应用目录中:

    unzip ~/Downloads/gdata-2.0.18.zip

    mv gdata-2.0.18/src/* .
    rm -rf gdata-2.0.18/

  12. 确保PyCrypto安装在本地(但不在您的应用程序目录中):

    sudo easy_install pycrypto
  13. 在您的中

    app.yaml
    ,将PyCrypto添加为库:

    libraries:
    • name: pycrypto
      version: “2.6”
  14. 声明以下帮助程序类:

    import httplib2

    class TokenFromOAuth2Creds:
    def init(self, creds):
    self.creds = creds
    def modify_request(self, req):
    if self.creds.access_token_expired or not self.creds.access_token:
    self.creds.refresh(httplib2.Http())
    self.creds.apply(req.headers)

  15. 使用私钥创建一个

    SignedJwtAssertionCredentials
    对象:

    from oauth2client.client import SignedJwtAssertionCredentials

    credentials = SignedJwtAssertionCredentials(
    “<service account email>@developer.gserviceaccount.com”,
    file(“secret-privatekey.pem”, “rb”).read(),
    scope=["http://www.google.com/m8/feeds/”],
    prn=”<user to impersonate>@your-domain.com“
    )

  16. 创建一个gdata客户端并使用它:

    gd_client = gdata.contacts.client.ContactsClient('your-domain.com')

    gd_client.auth_token = TokenFromOAuth2Creds(credentials)
    xxx = gd_client.get_contacts()



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5663260.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存