让AppEngine AppAssertionCredentials工作(Python)?

2024-09-22 16:38:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将一个appengine python项目从旧的googleadminisdkapi更新到新的,很难获得built in services accountAppAssertionCredentials)的身份验证。在

我已经有了

  • 在开发人员控制台中启用了管理SDK API
  • 在Google Apps仪表板中启用API访问

为了解决这个问题,我从开发人员控制台创建了一个服务帐户,下载了凭证.json,并在Google Apps仪表板(安全>高级设置>管理API客户端访问)中为该帐户设置以下权限(不带引号):

  • 客户端:“{service account id}。apps.googleusercontent.com““
  • {{a4}权限:^ a3}

我当前的代码是(稍作修改)

# ...

import json
from google.appengine.api import memcache
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client.client import SignedJwtAssertionCredentials

GAPPS_CREDENTIALS = 'credentials.json'
GAPPS_SCOPES = [
  'https://www.googleapis.com/auth/admin.directory.user.readonly',
  'https://www.googleapis.com/auth/admin.directory.group.readonly',
  'https://www.googleapis.com/auth/admin.directory.group.member.readonly'
]
GAPPS_ADMIN = 'admin'
GAPPS_DOMAIN = 'domain.com'
GAPPS_USER = 'user'

# ...

with open(GAPPS_CREDENTIALS) as jsonfile:
  jsondata = json.load(jsonfile)
  credentials = SignedJwtAssertionCredentials(
    jsondata['client_email'],
    jsondata['private_key'],
    scope=GAPPS_SCOPES,
    sub=GAPPS_ADMIN+'@'+GAPPS_DOMAIN
)

http = credentials.authorize(Http(memcache))
apps = build('admin', 'directory_v1', http=http)
user = apps.users().get(userKey=GAPPS_USER +'@'+GAPPS_DOMAIN ).execute()
console.log(user['email'])

# ...

这是完美的工作,但是如果我可以,我想消除凭证.json使用AppAssertionCredentials调用进行身份验证。在

但是,只要我运行以下代码:

^{pr2}$

我得到一个403错误:

<HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users/user%domain.com?alt=json returned "Not Authorized to access this resource/api">

我怀疑问题可能是内置服务帐户没有在Google Apps Dashboard中进行身份验证,但是我无法在开发者控制台或App Engine控制台中找到帐户域来添加这些权限。我试过:

  • 添加在“开发人员控制台”>;“权限”下找到的所有服务帐户,更改@developer.gserviceaccount.com到。apps.googleusercontent.com(在@cloudservices.gserviceaccount.com帐户导致错误:This client name has not been registered with Google yet.
  • 使用{project}。appspot.com网站域:Google Apps错误为This client name has not been registered with Google yet.

有人能给我们点启示吗?在

谢谢


Tags: appsfromimportcomclientjson权限admin
1条回答
网友
1楼 · 发布于 2024-09-22 16:38:25

要回答有关在何处找到App Engine默认服务帐户的电子邮件地址表单的问题:它位于新控制台中的“权限”->“服务帐户”(与API Manager->;“您自己创建的服务帐户的凭据”相对应)。在

现在来回答一个主要问题:AppAssertionCredentials不适用于具有委派访问权限的服务帐户,因为它does not accept a 'sub' parameter用于构造函数和doesn't have a method for creating delegated credentials(它不像SignedJwtAssertionCredentials那样用作关键字参数,现在是ServiceAccountCredentials,被忽略)。不过,您仍然可以使用appengine默认服务帐户进行委派,因为新控制台现在允许您create JSON keys for the default service accounts。在

使用AppAssertionCredentials支持委托访问是可能的,为此,您可以在project issue tracker上发布功能请求。在

相关问题 更多 >