使用 gdata-python-client 进行跨域委派

63 阅读2分钟

在企业应用中,您可能希望以编程方式访问用户数据,而无需用户进行任何手动授权。之前有一个名为 2LO(2 条腿 OAuth)的协议,但似乎它与已弃用的 OAuth1 相关:“重要提示:OAuth 1.0 已弃用,并且关闭了新的 OAuth 1.0 客户端的注册。”遍布整个 Oauth1 文档。针对“跨域委派”提供了一种新的基于 OAuth2 的方法:

huake_00210_.jpg 在 Google Apps 域中,域管理员可以授予第三方应用程序对其实施用户数据的域级访问权限——这被称为跨域委派。要以这种方式委派权限,域管理员可以使用具有 OAuth 2.0 的服务帐户。

虽然这适用于 google-api-python-client,但不适用于 gdata-python-client。

2、解决方案 以下是如何在 Google App Engine 中使用 gdata 库在 Python 中执行跨域委派的一个示例:

  1. 创建一个项目(cloud.google.com/console#/pr…
  2. 在“API 和身份验证”下启用您需要使用的 API(某些 gdata API 不会出现在此处,如果是这样,请跳过此步骤)。
  3. 在“API 和身份验证”->“凭据”下创建一个新的服务帐号 OAuth2 客户端 ID。记下电子邮件地址和客户端 ID,并将下载的私钥保存到安全的位置。
  4. 作为域管理员,转到管理控制台 (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. 从 code.google.com/p/google-ap… 下载 Google API Python 客户端,选择 google-api-python-client-gae-1.2.zip。 8. 在您的应用程序目录中解压缩此文件:

unzip ~/Downloads/google-api-python-client-gae-1.2.zip
  1. code.google.com/p/gdata-pyt… 下载 gdata python 客户端,选择 gdata-2.0.18.zip。
  2. 在您的应用程序目录中安装此文件:
unzip ~/Downloads/gdata-2.0.18.zip
mv gdata-2.0.18/src/* .
rm -rf gdata-2.0.18/
  1. 确保 PyCrypto 已在本地安装(但不在您的应用程序目录中):
sudo easy_install pycrypto
  1. 在您的 app.yaml 中,将 PyCrypto 添加为库:
libraries:
- name: pycrypto
  version: "2.6"
  1. 声明以下帮助程序类:
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)
  1. 使用私钥创建一个 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"
)
  1. 创建一个 gdata 客户端并使用它:
gd_client = gdata.contacts.client.ContactsClient('your-domain.com')
gd_client.auth_token = TokenFromOAuth2Creds(credentials)
xxx = gd_client.get_contacts()