2016-08-09 30 views
0

Google Admin SDKレポートV1 APIを使用して、Google Apps管理パネルからすべての管理アクティビティを収集しています。Google Admin SDKエラー401の認証時に

私のGoogleのAPI Pythonクライアントは、最近まで私は、次を使用していたバージョン"google-api-python-client (1.5.1)"

に更新されました:

from oauth2client.client import SignedJwtAssertionCredentials 

serviceAccountEmail = "[email protected]" 
key = "google-apps-file.p12" 
scopes = ['https://www.googleapis.com/auth/admin.reports.audit.readonly'] 


credentials = SignedJwtAssertionCredentials(
    serviceAccountEmail, key, scope=scopes, sub=userEmail) 

その後、GoogleはSignedJwtAssertionCredentialsのサポートを落としました。だから私はこれに切り替えました。

from oauth2client.service_account import ServiceAccountCredentials 

serviceAccountEmail = "[email protected]" 
key = "google-apps-file.p12" 
scopes = ['https://www.googleapis.com/auth/admin.reports.audit.readonly'] 

credentials = ServiceAccountCredentials.from_p12_keyfile(
    serviceAccountEmail, key, scopes=scopes)` 

これは比較的簡単なコード変更であるはずですが、コードを実行すると次のエラーが発生します。

File "/usr/local/lib/python2.7/site-packages/oauth2client/util.py", line 137, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/site-packages/googleapiclient/http.py", line 832, in execute 
    raise HttpError(resp, content, uri=self.uri) 
googleapiclient.errors.HttpError: <HttpError 401 when requesting https://www.googleapis.com/admin/reports/v1/activity/users/all/applications/admin?alt=json returned "Access denied. You are not authorized to read activity records."> 

だから、何の権限は私も気付いています一つのことは、元のコードがsub=userEmailを求めてさ(偽装するアカウントである、そのアカウントをGoogle Appsドメイン上の特定の管理者権限を持っているでしょう。)、変更されていません

私は401が得られるはずですが、新しいドキュメントにはsub=userEmailパラメータの記載がありません。

答えて

0

私は何が欠けていたのか分かりました。

もともと私は

f = open(serviceAccountKeyLocation, 'rb') 
key = f.read() 
f.close() 
# Packaging up the auth parameters to pass along with the request. 

credentials = SignedJwtAssertionCredentials(serviceAccountEmail, key, scope=scopes, sub=userEmail) 
http = credentials.authorize(http) 
return http 

を持っていた私は、いくつかのアップ読み、さらに研究を行ないました。

次にこれで置き換えます。

credentials = ServiceAccountCredentials.from_p12_keyfile(serviceAccountEmail, 
                  serviceAccountKeyLocation, 
                  scopes=scopes) 
delegate_credentials = credentials.create_delegated(userEmail) # this fixes the sub user 
http = delegate_credentials.authorize(http) 
return http 

私は新しい更新されたバージョンがfrom oauth2client.service_account import ServiceAccountCredentialsを使用しているが、まだfrom oauth2client.client import SignedJwtAssertionCredentialsを利用した実行していたグーグルのAPI Pythonクライアントのバージョン。

関連する問題