簡體   English   中英

讓AppEngine AppAssertionCredentials正常工作(Python)?

[英]Getting AppEngine AppAssertionCredentials working (Python)?

我正在將舊的Google Admin SDK API中的App Engine python項目更新為新的,並且難以獲得內置服務帳戶AppAssertionCredentials )的身份驗證。

我已經

  • 在Developers控制台中啟用了Admin SDK API
  • 在Google Apps信息中心中啟用API訪問權限

為了解決這個問題,我從開發者控制台創建了一個服務帳戶,下載了credentials.json,並在該帳戶的Google Apps信息中心(安全性>高級設置>管理API客戶端訪問權限)中設置了以下權限(不含引號):

我目前的代碼是(稍微編輯)

# ...

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'])

# ...

這很好用,但是如果可以的話,我想通過使用AppAssertionCredentials調用來驗證憑證來消除credentials.json文件。

但是,只要我運行以下代碼:

# ...

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

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'

# ...

credentials = AppAssertionCredentials(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'])

# ...

我收到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信息中心中進行身份驗證,但我無法在開發人員控制台或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.

誰能擺脫任何光明?

謝謝

要回答有關在何處查找App Engine默認服務帳戶的電子郵件地址表單的問題:這位於新控制台的權限 - >服務帳戶下(而不是API管理器 - >您自己創建的服務帳戶的憑據)。

現在回答主要問題:AppAssertionCredentials不適用於具有委派訪問權限的服務帳戶,因為它不接受構造函數的“sub”參數也沒有創建委派憑據的方法 (它不用作關鍵字與SignedJwtAssertionCredentials一樣的參數,現在是ServiceAccountCredentials ,並被忽略)。 盡管如此,您仍然可以使用App Engine默認服務帳戶,因為新控制台現在允許您為默認服務帳戶創建JSON密鑰

可以通過AppAssertionCredentials支持委派訪問,為此您可以在項目問題跟蹤器上發布功能請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM