google-code-export / google-api-python-client

Automatically exported from code.google.com/p/google-api-python-client
Other
1 stars 0 forks source link

403 error / Forbidden when accessing https://www.googleapis.com/auth/plus.profiles.read in python #337

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I had a working application that pulled all users from a domain and grab the 
user picture from Google Plus. After few months it was giving me a "403 error / 
Forbidden when accessing"

This is the code

import httplib2
import pprint

from apiclient.discovery import build

from oauth2client.client import SignedJwtAssertionCredentials

SERVICE_ACCOUNT_EMAIL = 'xxxx@developer.gserviceaccount.com'
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/xxxx/privatekey.pem'
USER_EMAIL = 'xyz@test.com'

SCOPES = ['https://www.googleapis.com/auth/plus.profiles.read']

def authenticate():
    print "opening the key"
    f = open(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
    key = f.read()
    f.close()
    print "closing the key"

    credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key, scope=SCOPES, sub=USER_EMAIL)
    http = httplib2.Http()
    http = credentials.authorize(http)

    print "getting the credentials"
    return build('plusDomains', 'v1', http=http)

print "calling the authentication"
service = authenticate()

print "calling people service"
people_service = service.people()

print "getting the user profile"
people_document = people_service.get(userId='abc@test.com').execute()

print 'ID: %s' % people_document.get('id')
print 'Display name: %s' % people_document.get('displayName')
print 'Image URL: %s' % people_document.get('image').get('url')
print 'Profile URL: %s' % people_document.get('url')

This is the output:
calling the authentication
opening the key
closing the key
getting the credentials
calling people service
getting the user profile
Traceback (most recent call last):
  File "test.py", line 39, in <module>
    people_document = people_service.get(userId='abc@test.com').execute()
  File "/usr/local/lib/python2.7/dist-packages/oauth2client/util.py", line 132, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/apiclient/http.py", line 723, in execute
    raise HttpError(resp, content, uri=self.uri)
apiclient.errors.HttpError: <HttpError 403 when requesting 
https://www.googleapis.com/plusDomains/v1/people/abc@test.com?alt=json returned 
"Forbidden">

By the way this is the scopes I have in the Google's CPANEL

https://www.googleapis.com/auth/admin.directory.user.readonly
https://www.googleapis.com/auth/plus.profiles.read

Here is the link to the stackoverflow question: 
http://stackoverflow.com/questions/25857123/403-error-forbidden-when-accessing-h
ttps-www-googleapis-com-auth-plus-profil

Original issue reported on code.google.com by adinno...@conservation.org on 23 Sep 2014 at 6:10