HttpError 400错误请求-Google Admin Directory API(Python)

水星侠

我在创建Google Project即服务帐户时遇到困难。我正在Python中使用Admin SDK,尤其是Directory API。我相信我的身份验证正确,但是在调用users.list时出现以下错误:

Traceback (most recent call last):
  File "googleproject.py", line 17, in <module>
userlist = service.users().list().execute(http = http_auth)
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 135, in positional_wrapper
return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/googleapiclient/http.py", line 723, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/admin/directory/v1/users?alt=json returned "Bad Request">

我的代码如下:

from oauth2client.client import SignedJwtAssertionCredentials
from httplib2 import Http
from apiclient.discovery import build

#----AUTHORISATION----#

client_email = '*****@developer.gserviceaccount.com'
with open('*****.p12') as f:
  private_key = f.read()

credentials = SignedJwtAssertionCredentials(client_email, private_key, 'https://www.googleapis.com/auth/admin.directory.user')
http_auth = credentials.authorize(Http())

#--------------------#

service = build('admin', 'directory_v1', http = http_auth)
userlist = service.users().list().execute(http = http_auth)

我已经有和没有经过尝试它http = http_auth作为参数execute()我基本上遵循此处给出的示例:https : //code.google.com/p/google-api-python-client/source/browse/samples/service_account/tasks.py

我已经在开发人员控制台中启用了Admin SDK,并在Google Apps控制面板中添加了客户端ID和范围。

水星侠

我设法解决了!问题是我没有在list参数中设置域。因此,新代码如下:

from oauth2client.client import SignedJwtAssertionCredentials
from httplib2 import Http
from apiclient.discovery import build

#----AUTHORISATION----#

client_email = '*****@developer.gserviceaccount.com'
with open('*****') as f:
  private_key = f.read()

credentials = SignedJwtAssertionCredentials(client_email, private_key, 'https://www.googleapis.com/auth/admin.directory.user', sub = '[email protected]')
http_auth = credentials.authorize(Http())

#--------------------#

service = build('admin', 'directory_v1', http = http_auth)
user = service.users().list(showDeleted = False, domain = 'domain.com').execute()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

urllib2.HTTPError:HTTP错误400:错误的请求-Python

urllib2.HTTPError:HTTP错误400:错误的请求-django ping_google

urllib.error.HTTPError:HTTP 错误 400:Python 函数中的错误请求

Google Directory API用户列表请求失败,输入400无效

Google API和Directory SDK:获取每个域的组数:HttpError(“ group”)

服务器端Google API服务请求会产生googleapiclient.errors.HttpError:<HttpError 404

在API请求上获取HTTPError

requests.exceptions.HTTPError:400客户端错误:错误的网址请求:https://accounts.spotify.com/api/token

重复规则错误400错误的请求-Google Api日历

Google Sheets API HttpError 500和503

Google 400错误的请求使用curl转换免费的api

Google Drive API使用PageToken返回400错误请求

Google 工作区 ADMIN SDK Directory API 返回 403

再次urllib.error.HTTPError:HTTP错误400:错误的请求

HTTPError问题:urllib.request.urlopen python请求错误

urllib.error.HTTPError: HTTP 错误 404: Not Found using Google Matrix API

向Google Maps API发出GET请求,返回错误请求400错误-javascript

C# - Google API 请求 access_token - 400 - 错误请求

向Google gmail API发出令牌请求时,请求400错误

Python请求400错误

来自Google的400错误请求

需要使用Google ServiceAccountCredential使用Google Admin Directory API登录401

在Google Talent Solutions API上遇到400错误的请求错误-Symfony 4

Google电子表格API,400错误的错误请求:无法解析范围

NextPageToken给出无效的输入错误400-Google Directory API ChomeDevices

为什么我的示例 google api 代码(python、admin、directory_v1)上的 503 服务不可用?

使用服务帐户时 Google Calendar API HttpError 404

使用Google Admin Directory API添加组-未授权403访问此资源/ API

使用Google Admin SDK Directory API时获取“域无法使用api”