2016-08-09 4 views
0

Google Admin SDK Reports V1 API를 사용하여 Google Apps 관리자 패널에서 모든 관리 활동을 수집하고 있습니다. 승인을 시도 할 때 Google Admin SDK 오류 401

내 구글 API를 파이썬 클라이언트

는 최근 이전에 나는 다음과 같은 사용 된 버전 "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) 

그런 다음 구글 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를 파이썬 클라이언트의 버전입니다.

관련 문제