googleoauth客户端使用了json文件Python中错误的项目id

2024-06-16 11:39:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Python(3.6.7)代码使用oauth2client访问googlephotosapi。它成功地进行了身份验证,但是当它尝试访问Google相册时,它似乎使用用户名作为项目的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']))

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

HttpError 403 when requesting {URL} returned "Photos Library API has not been used in project 123456 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/photoslibrary.googleapis.com/overview?project=123456 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">

有趣的是,粗体数字123456(明显改变)实际上是在客户机中找到的客户机id的第一部分_标识.json你知道吗

但是项目id看起来是这样的:test1-235515

所以我从这个错误中得到的是oauth2client传递的是client\u id而不是project\u id。你知道吗

请帮助解决此错误。如何手动更改项目id?你知道吗


Tags: the项目fromimportcomclient身份验证api
1条回答
网友
1楼 · 发布于 2024-06-16 11:39:58

项目ID与项目编号不同。您将能够在您的Google云控制台配置中看到这两者。有关如何识别项目的更多信息,请参阅本文档[1]。你知道吗

单个Google云项目可以配置许多不同的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

相关问题 更多 >