错误:客户端未经授权检索访问令牌(ServiceAccountCredentials)

时间:2017-03-30 12:25:40

标签: google-app-engine google-api credentials

我最近更新了SignedJwtAssertionCredentials to ServiceAccountCredentials

代码段:

SUB = "XXXXXXXXXXXXXXXX@XXXXXXXXX.com"
scopes = ["XXXXXXXXXXXXXXXX]

json_file = os.path.join(os.path.dirname(__file__), "XXXXXXXXX.json")
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scopes=scopes)

delegated_credentials = credentials.create_delegated(SUB)
http = httplib2.Http()
http = delegated_credentials.authorize(http)
return build('admin', 'directory_v1', http=http)

错误:

  

文件" /base/data/home/apps/project_name/main.py",第867行,授权           返回构建(' admin',' directory_v1',http = http)       在position_wrapper中的文件" /base/data/home/apps/project_name/lib/oauth2client/util.py",第128行           返回包裹(* args,** kwargs)       文件" /base/data/home/apps/project_name/lib/apiclient/discovery.py" ;,第193行,在构建中           resp,content = http.request(requested_url)       new_request中的文件" /base/data/home/apps/project_name/lib/oauth2client/transport.py" ;,第159行           credentials._refresh(orig_request_method)       文件" /base/data/home/apps/project_name/lib/oauth2client/client.py" ;,第744行,在_refresh中       self._do_refresh_request(HTTP)       文件" /base/data/home/apps/project_name/lib/oauth2client/client.py",第812行,在_do_refresh_request中       引发HttpAccessTokenRefreshError(error_msg,status = resp.status)       HttpAccessTokenRefreshError:unauthorized_client:客户端未经授权使用此方法检索访问令牌。

我做了什么:

我使用客户端ID( Google Cloud Platform > IAM& Admin > 服务帐户&gt>授权domain-wide authority ; View Client ID )我在documentation之后在G Suite域管理控制台中授权。

enter image description here

我认为这可以解决问题,但第二天,同样的错误消息。问题是什么?

1 个答案:

答案 0 :(得分:0)

看起来oauth2client构建正在尝试刷新访问令牌,但服务帐户无法使用该方法刷新子帐户凭据的访问令牌。

如果您跳过两条http线并执行以下操作该怎么办:

SUB = "XXXXXXXXXXXXXXXX@XXXXXXXXX.com"
scopes = ["XXXXXXXXXXXXXXXX]

json_file = os.path.join(os.path.dirname(__file__), "XXXXXXXXX.json")
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file, scopes=scopes)

delegated_credentials = credentials.create_delegated(SUB)
return build('admin', 'directory_v1', credentials=delegated_credentials)

传递credentials而不是授权的http对象。