【问题标题】:Not being able to authenticate service accounts with AppAssertionCredentials on App Engine for a Gmail service无法使用 App Engine 上的 AppAssertionCredentials 对 Gmail 服务的服务帐户进行身份验证
【发布时间】:2014-09-12 09:20:38
【问题描述】:

我正在尝试构建一个 Gmail 服务,该服务将在用户的 IT 管理员在应用市场上对应用进行身份验证后读取用户的电子邮件。从文档来看,服务帐户似乎是合适的选择,我尝试了这两种方法:

    scope = "https://www.googleapis.com/auth/gmail.readonly"
    project_number = "c****io"
    authorization_token, _ = app_identity.get_access_token(scope)
    logging.info("Using token %s to represent identity %s",
                 authorization_token, app_identity.get_service_account_name())
    #authorization_token = "OAuth code pasted from playground"
    response = urlfetch.fetch(
        "https://www.googleapis.com/gmail/v1/users/me/messages",
        method=urlfetch.GET,
        headers = {"Content-Type": "application/json",
                   "Authorization": "OAuth " + authorization_token})

    credentials = AppAssertionCredentials(scope=scope)
    http = credentials.authorize(httplib2.Http(memcache))
    service = build(serviceName='gmail', version='v1', http=http)
    listReply = gmail_service.users().messages().list(userId='me', q = '').execute()

然后我按照Unable to access BigQuery from local App Engine development server 启动了 dev_appserver.py

但是,我收到 HTTP 错误代码 500“后端错误”。相同的代码,但是当我从 OAuth 游乐场粘贴 access_token 时,它工作正常(HTTP 200)。我在我的本地机器上,以防万一。想知道我是否遗漏了什么?我正在尝试查找其 IT 管理员安装了我的 Google Marketplace 应用程序的特定域的所有用户的所有电子邮件。

感谢您的帮助!

【问题讨论】:

    标签: google-app-engine google-oauth google-api-python-client gmail-api


    【解决方案1】:

    要进行这种类型的模拟,您应该创建一个 JWT 并将“sub”字段设置为您要访问其邮箱的用户的电子邮件地址。开发者文档:Using OAuth 2.0 for Server to Server Applications: Additional claims

    用于构造凭据的 Python 代码类似于

    credentials = SignedJwtAssertionCredentials(
        "<service account email>@developer.gserviceaccount.com",
        file("secret-privatekey.pem", "rb").read(),
        scope=["https://www.googleapis.com/auth/gmail.readonly"],
        sub="<user to impersonate>@your-domain.com"
    )
    

    【讨论】:

    • 谢谢!因此,在 Google Apps Marketplace 应用程序(我们正在构建)的上下文中,这应该适用于已安装该应用程序的任何域吗?另外,我注意到您可以创建多个服务帐户...这适用于任何 (SERVICE_ACCOUNT_EMAIL, private_key.p12) 组合吗?我问是因为许多域范围的身份验证委托文档都在谈论“AdminConsole:管理第三方 OAuth 客户端访问”,但不是市场应用程序的情况。当我使用“测试安装流程”在我的域上安装应用程序时,API 客户端访问会显示应用程序名称而不是服务帐户电子邮件。
    猜你喜欢
    • 2020-06-24
    • 2019-05-01
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-20
    相关资源
    最近更新 更多