people.connections.list未使用Google-People API在Python中返回个人资料详细信息和联系人

时间:2019-01-02 10:19:19

标签: python-3.x http oauth google-people

我已通过创建凭据使用oauth2.0在Google控制台中连接了Google的People-API。而且,当我尝试通过在Google中使用此API获取用户的个人资料详细信息和联系方式时,却无法获取。以下是用于身份验证和获取数据的示例代码。

import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.tools import run_flow

# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for
# installed applications.
#
# Go to the Google API Console, open your application's
# credentials page, and copy the client ID and client secret.
# Then paste them into the following code.
FLOW = OAuth2WebServerFlow(
    client_id='xxxxxxxxx-xxxxxxxxxxxxxxxxx',
    client_secret='xxxxxxxxxxxxxxxxxxxx',
    scope='https://www.googleapis.com/auth/contacts.readonly',
    user_agent='myapp/2.0',
    redirect_uri='http://localhost')

# If the Credentials don't exist or are invalid, run through the
# installed application flow. The Storage object will ensure that,
# if successful, the good Credentials will get written back to a
# file.
storage = Storage('info.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
  credentials = run_flow(FLOW, storage)

# Create an httplib2.Http object to handle our HTTP requests and
# authorize it with our good Credentials.
http = httplib2.Http()
http = credentials.authorize(http)

# Build a service object for interacting with the API. To get an API key for
# your application, visit the Google API Console
# and look at your application's credentials page.
people_service = build(serviceName='people', version='v1', http=http)

connections = people_service.people().connections().list('people/me', pageSize=100, personFields='names,emailAddresses').execute()
profile = people_service.people().get('people/me', pageSize=100, personFields='names,emailAddresses').execute()

运行以上代码后。我收到以下错误。

Traceback (most recent call last):
  File "people_api_auth.py", line 43, in <module>
    connections = people_service.people().connections().list('people/me', pageSize=100, personFields='names,emailAddresses').execute()
TypeError: method() takes 1 positional argument but 2 were given

谁能解释为什么我遇到此错误。 任何帮助将不胜感激。 谢谢!!!

1 个答案:

答案 0 :(得分:0)

看看位于https://developers.google.com/people/quickstart/python的示例

好像他们使用list(resourceName='people/me'而不是list('people/me'