2013-12-20 4 views
1

사용자의 eamil 계정을 만들려면 admin sdk directory api를 사용하고 싶습니다.google admin sdk directory api 403 python

저는 google-api-python-client-1.2 라이브러리를 사용하고 있습니다.

/samples/service_account/tasks.py 폴더에 있습니다.

하지만 관리자 디렉토리에서 사용자를 나열 할 수있는 기회가있는 경우 작동하지 않으며 오류가 발생합니다.

아래 코드는 제가 사용하고있는 코드입니다.

import httplib2 
import pprint 
import sys 
import inspect 

from apiclient.discovery import build 
from oauth2client.client import SignedJwtAssertionCredentials 
def main(argv): 
    f = file('my-privatekey.p12', 'rb') 
    key = f.read() 
    f.close() 

    credentials = SignedJwtAssertionCredentials(
     '[email protected]', 
     key, 
     scope=['https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.user.readonly']) 
    http = httplib2.Http() 
    http = credentials.authorize(http) 

    service = build("admin", "directory_v1", http) 
    list_of_apis = service.users().list(domain='mydomain.com').execute(http=http) 
    pprint.pprint(list_of_apis) 
if __name__ == '__main__': 
     main(sys.argv) 

위의 코드를 실행할 때 오류가 발생합니다.

$python tasks.py 
No handlers could be found for logger "oauth2client.util" 
Traceback (most recent call last): 
    File "tasks.py", line 77, in <module> 
    main(sys.argv) 
    File "tasks.py", line 66, in main 
    list_of_apis = service.users().list(domain='messycoders.com').execute(http=http) 
    File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 723, in execute 
    raise HttpError(resp, content, uri=self.uri) apiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?domain=messycoders.com&alt=json returned "Not Authorized to access this resource/api"> 

답변

3

시도 : 당신은 당신이 읽고 쓰기하고있는 경우에만, 위의를 사용하여 읽기 작업을하고 있다면 당신은 모두 범위가 필요하지 않습니다

credentials = SignedJwtAssertionCredentials(
     '[email protected]', 
     key, 
     sub='[email protected]', 
     scope=['https://www.googleapis.com/auth/admin.directory.user',]) 

는 읽기 전용 사용합니다.

sub = 디렉토리 서비스를 수행하기 위해 서비스 계정이 가장해야하는 Google Apps 계정을 정의합니다. 필요하며 계정에 올바른 권한이 있어야합니다.

마지막으로 제어판에서 필요한 디렉터리 범위에 서비스 계정의 client_id 액세스 권한을 부여했는지 확인하십시오. steps to do this are listed in the Drive documentation은 관리 디렉토리의 올바른 범위에 속합니다.

+0

여전히 작동하지 않습니다. 동일한 오류가 발생합니다. 위의 스크립트가 내 로컬 시스템에서도 작동합니까? 아니면 등록 된 도메인에서 실행해야합니까? –

+0

다음 사항을 확인했습니다. 1) 클라우드 콘솔의 서비스 계정 페이지에서 다운로드 한 client_secret 파일에 표시된 것처럼 서비스 계정 client_id에 messycoders.com 도메인의 Admin SDK 사용자 범위가 부여 되었습니까? 2) sub =에 사용하는 주소는 도메인의 최고 관리자입니까? –

+0

서비스 계정은 어디에서나 작동해야합니다. –

관련 문제