4
from apiclient.discovery import build 
from oauth2client.service_account import ServiceAccountCredentials 
import json 
import base64 
import httplib2 

f = file('tara1-553d334b8702.p12', 'rb') 
key = f.read() 
f.close() 

credentials = ServiceAccountCredentials.from_p12_keyfile(
    '[email protected]', 
    'tara1-554d335b8702.p12', 
    private_key_password='notasecret', 
    scopes=['https://www.googleapis.com/auth/admin.directory.user.readonly','https://www.googleapis.com/auth/admin.directory.user'] 
) 

delegated_credentials=credentials.create_delegated('[email protected]) 

http = httplib2.Http() 
http = credentials.authorize(http) 


DIRECOTORY = build('admin', 'directory_v1', credentials=credentials) 
maxResults=10,orderBy='email').execute() 
results = DIRECOTORY.users().list(domain='domainnamehere.net').execute() 
users = results.get('users', []) 

print users 

在這裏,我想用P12安全文件做服務器到服務器的身份驗證,並試圖搶在域中的所有用戶。嘗試通過P12訪問谷歌目錄API拋出未授權的錯誤

我已經成功地獲取由3legs身份驗證的用戶列表中,從browswer授權在同一個帳戶

但是這樣一來它拋出我下面的錯誤。

File "testing.py", line 41, in <module> 
    results = DIRECOTORY.users().list(domain='domainemailhere.net').execute() 
    File "/home/tara/taraproject/scripttesting/virtualenvforGapi/local/lib/python2.7/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/home/tara/taraproject/scripttesting/virtualenvforGapi/local/lib/python2.7/site-packages/googleapiclient/http.py", line 840, in execute 
    raise HttpError(resp, content, uri=self.uri) 
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/admin/directory/v1/users?domain=nepallink.net&alt=json returned "Not Authorized to access this resource/api"> 

設置DONE:

  1. 我在管理控制檯超級管理員級別的訪問。

  2. 我也通過安全添加 範圍>相冊更多>>暫>高級> manageipclient>授權 添加的用戶ID和範圍

    https://www.googleapis.com/auth/admin.directory.user.readonly https://www.googleapis.com/auth/admin.directory.user

  3. 新增的用戶權限服務控制檯,併成爲一名業主。

  4. 聯繫SDK爲Enabled

確切位置在哪裏我失去的東西。爲什麼它說我沒有權限訪問資源/ api

回答

2

我看到您使用的是delegated_credentials。你用過嗎?

更改以下行:

DIRECOTORY = build('admin', 'directory_v1', credentials=credentials) 

DIRECOTORY = build('admin', 'directory_v1', credentials=delegated_credentials) 
+0

感謝了很多,CRAZY愚蠢的錯誤,裝箱一個代表,但沒有使用它。謝謝 –

+0

我不是API人員,但是值得看看下面的鏈接: [got-403-error-when-connecting-to-google-analytics-with-python-2-7-x]( http://stackoverflow.com/questions/27674380/got-403-error-when-connecting-to-google-analytics-with-python-2-7-x),[快速指南](https://開發人員。 google.com/analytics/devguides/config/mgmt/v3/quickstart/service-py ) – Err0rr

相關問題