Search code examples
pythongoogle-api-clientgoogle-plus-domains

403 Unauthorized when using Google+ API in Python


I'm using this code, mostly extracted from Google samples:

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

You can see I have some functions that return the credentials generated from Google's developers console.

credentials_console_screenshot

I get the red and *-masked string for client_id and the one client_secret inside the JSON downloaded using the right red download button.

So everything goes OK at first, it opens a windows in Chrome and I consent and paste the code at the command-line, and when coming to this code:

request.execute()

it shows an error trace:

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

I did a similar code with the basic G+ API and got it working, but with this I'm stuck. I don't know where is the error. The only clue I've got is that this import appears as unresolved in PyCharm:

from apiclient.discovery import build

Any ideas? I can't find where I'm not following the Google API instructions. I keep reading but I've got no clue.


Solution

  • I'm now pretty sure that this can't be done because G+ API is not being maintained.

    I'm accepting this as answer but if anyone does come up with a solution I think I can change the accepted answer so, be my guest.