在python中访问https://www.googleapis.com/auth/plus.profiles.read时出现403错误/禁止访问

时间:2014-09-15 21:24:04

标签: python google-api google-plus google-api-python-client

当我尝试访问用户个人资料时,我收到了“HttpError 403”。我按照教程设置了中央面板中的所有权限。以下是代码:

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

以下是回复:

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

顺便说一下,这是我在Google的CPANEL中的范围

我们之前有这个工作,但是因为一个月前没有工作。欢迎任何帮助。

0 个答案:

没有答案