Google OAuth客户端使用了来自json文件Python

时间:2019-03-24 17:35:20

标签: python json api google-oauth google-photos-api

我的Python(3.6.7)代码使用oauth2client访问Google Photos API。它成功进行了身份验证,但是当它尝试访问Google相册时,似乎使用了用户名作为project_id。

from __future__ import print_function
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

# Setup the Photo v1 API
SCOPES = 'https://www.googleapis.com/auth/photoslibrary.readonly'
store = file.Storage('credentials.json')
creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('scripts/client_id.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('photoslibrary', 'v1', http=creds.authorize(Http()))

# Call the Photo v1 API
results = service.albums().list(
    pageSize=10, fields="nextPageToken,albums(id,title)").execute()
items = results.get('albums', [])
if not items:
    print('No albums found.')
else:
    print('Albums:')
    for item in items:
        print('{0} ({1})'.format(item['title'].encode('utf8'), item['id']))

执行上述代码时,它会提示我身份验证页面。成功认证后,它会显示以下错误:

  

在请求{URL}时返回HttpError 403,“之前项目 123456 中未使用过照片库API或已将其禁用。请访问https://console.developers.google.com/apis/api/photoslibrary.googleapis.com/overview?project= 123456 然后重试。如果您最近启用了此API,请等待几分钟,以使操作传播到我们的系统并重试。“>

有趣的是,粗体 123456 (显然已更改)中的数字实际上是在client_id.json中找到的client_id的第一部分

但是project_id看起来像这样: test1-235515

所以我从此错误中得到的是oauth2client客户端正在传递client_id而不是project_id。因此,即使我启用了Photos API,也永远无法正确访问它。

请帮助解决此错误。如何手动更改project_id?

1 个答案:

答案 0 :(得分:0)

项目ID与项目编号不同。您将可以在Google Cloud Console配置中看到两者。有关如何识别项目的更多信息,请参见本文档[1]。

单个Google Cloud项目可以配置许多不同的OAuth客户端ID。有关创建OAuth客户端凭据[2]的信息,请参见本文档。您只需要确保您创建的客户端属于您已启用API的项目即可。转到错误消息中提供的URL,应该会将您带到正确的配置页面。

[1] https://cloud.google.com/resource-manager/docs/creating-managing-projects#identifying_projects

[2] https://support.google.com/cloud/answer/6158849?hl=en