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

问题描述 投票:0回答:1

我正在使用此代码,主要是从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)

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

credentials_console_screenshot

我得到了client_id的红色和*掩码字符串以及使用正确的红色下载按钮下载的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说明的地方。我一直在读,但我不知道。

python google-api-client google-plus-domains
1个回答
0
投票

我现在非常确定无法做到这一点,因为没有维护G + API。

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

© www.soinside.com 2019 - 2024. All rights reserved.