403 在 Python 中使用 Google+ API 时未经授权

403 Unauthorized when using Google+ API in Python

我正在使用这段代码,主要是从 Google 个样本中提取的:

"""
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)

你可以看到我有一些函数 return 从 Google's developers console 生成的凭据。

我使用右侧的红色下载按钮下载了 client_id 的红色和 *-masked 字符串以及 JSON 中的 client_secret。

所以一开始一切正常,它在 Chrome 中打开一个 windows 我同意并在命令行粘贴代码,当来到这个代码时:

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 说明的地方。我一直在看,但我一点头绪都没有。

我现在很确定这无法完成,因为 G+ API 没有得到维护。

我接受这个作为答案,但如果有人提出解决方案,我想我可以更改已接受的答案,所以请做客。