我正在使用这段代码,主要是从谷歌样本中提取的:
"""
did a pip3 install --upgrade google-api-python-client
"""
import httplib2
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from environments import get_client_id, get_client_secret
# List the scopes your app requires:
SCOPES = ['https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/plus.stream.write']
# The following redirect URI causes Google to return a code to the user's
# browser that they then manually provide to your app to complete the
# OAuth flow.
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# For a breakdown of OAuth for Python, see
# https://developers.google.com/api-client-library/python/guide/aaa_oauth
# CLIENT_ID and CLIENT_SECRET come from your API Console project
flow = OAuth2WebServerFlow(client_id=get_client_id(), # extracted from console.developers.google.com
client_secret=get_client_secret(), # extracted from console.developers.google.com
scope=SCOPES,
redirect_uri=REDIRECT_URI)
auth_uri = flow.step1_get_authorize_url()
# This command-line server-side flow example requires the user to open the
# authentication URL in their browser to complete the process. In most
# cases, your app will use a browser-based server-side flow and your
# user will not need to copy and paste the authorization code. In this
# type of app, you would be able to skip the next 3 lines.
# You can also look at the client-side and one-time-code flows for other
# options at https://developers.google.com/+/web/signin/
print('Please paste this URL in your browser to authenticate this program.')
print(auth_uri)
code = input('Enter the code it gives you here: ')
# Set authorized credentials
credentials = flow.step2_exchange(code)
# Create a new authorized API client.
http = httplib2.Http()
http = credentials.authorize(http)
service = build('plusDomains', 'v1', http=http)
circle_service = service.circles()
request = circle_service.list(userId='me')
while request is not None:
circle_list = request.execute()
if circle_list.get('items') is not None:
print('Google+ circles for the current user: ')
circles = circle_list.get('items')
for circle in circles:
print('\t %s' % circle.get('displayName'))
request = circle_service.list_next(request, circle_list)
您可以看到我有一些函数可以返回从Google 开发者控制台生成的凭据。
我得到了 client_id 的红色和 *-masked 字符串,以及使用右侧红色下载按钮下载的 JSON 中的一个 client_secret。
所以一开始一切都很顺利,它在 Chrome 中打开一个窗口,我同意并将代码粘贴到命令行,当来到这个代码时:
request.execute()
它显示错误跟踪:
Traceback (most recent call last):
File "/home/madtyn/PycharmProjects/telegram/api_samples/plus/sample_list_circles.py", line 62, in <module>
circle_list = request.execute()
File "/home/madtyn/.local/lib/python3.6/site-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/madtyn/.local/lib/python3.6/site-packages/googleapiclient/http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/plusDomains/v1/people/me/circles?alt=json returned "Forbidden">
我用基本的 G+ API 做了一个类似的代码并让它工作,但是我被困住了。我不知道错误在哪里。我得到的唯一线索是这个导入在 PyCharm 中显示为未解决:
from apiclient.discovery import build
有任何想法吗?我找不到没有遵循 Google API 说明的地方。我一直在读,但我没有任何线索。