2016-07-31 20 views
11

我试图以编程方式访问使用Python客户端库我自己的个人帐户谷歌联系人列表中的联系人people.connections.list没有返回使用Python客户端库

这是将上运行脚本服务器没有用户输入,所以我设置了使用来自我设置的服务帐户的凭证。我的Google API控制台设置看起来像这样。

enter image description here

我用下面简单的脚本,从API文档中提供的例子拉 -

import json 
from httplib2 import Http 

from oauth2client.service_account import ServiceAccountCredentials 
from apiclient.discovery import build 

# Only need read-only access 
scopes = ['https://www.googleapis.com/auth/contacts.readonly'] 

# JSON file downloaded from Google API Console when creating the service account 
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'keep-in-touch-5d3ebc885d4c.json', scopes) 

# Build the API Service 
service = build('people', 'v1', credentials=credentials) 

# Query for the results 
results = service.people().connections().list(resourceName='people/me').execute() 

# The result set is a dictionary and should contain the key 'connections' 
connections = results.get('connections', []) 

print connections #=> [] - empty! 

当我打的API返回,没有任何“关系”的结果集键。具体而言,它返回 -

>>> results 
{u'nextSyncToken': u'CNP66PXjKhIBMRj-EioECAAQAQ'} 

是否有某些与我的设置或代码有关的错误?有没有办法查看响应HTTP状态代码或获取关于它想要做什么的更多细节?

谢谢!

旁注:当我尝试使用the "Try it!" feature in the API docs,它正确返回我的联系人。虽然我怀疑是否使用客户端库,而是依赖于通过OAuth进行的用户授权

+0

嘿,我有完全一样的问题。你能解决它吗?谢谢。 – katericata

+0

@katericata - 我没有,对不起:( – user2490003

回答

5

personFields mask是必需的。指定一个或多个有效路径。有效的路径记录在https://developers.google.com/people/api/rest/v1/people.connections/list/

此外,使用字段掩码指定哪些字段包含在部分响应中。

相反的:

results = service.people().connections().list(resourceName='people/me').execute() 

...尝试:

results = service.people().connections().list(resourceName='people/me',personFields='names,emailAddresses',fields='connections,totalItems,nextSyncToken').execute() 
0

随着服务帐户,在DWD - 摹套房域范围内的代表团,在这种方式

必要模仿或委派用户
delegate = credentials.create_delegated('[email protected]') 
0

这里是一个工作演示。我现在只是测试它。 Python 3.5.2

google-api-python-client==1.6.4 
httplib2==0.10.3 
oauth2client==4.1.2 

你可以将它保存到demo.py然后运行它。我离开了create_contact函数,以防您可能想要使用它,并且还有一个关于API使用情况的示例。

CLIENT_IDCLIENT_SECRET是环境变量,所以我不小心共享代码。

"""Google API stuff.""" 

import httplib2 
import json 
import os 

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


CLIENT_ID = os.environ['CLIENT_ID'] 
CLIENT_SECRET = os.environ['CLIENT_SECRET'] 
SCOPE = 'https://www.googleapis.com/auth/contacts' 
USER_AGENT = 'JugDemoStackOverflow/v0.1' 

def make_flow(): 
    """Make flow.""" 
    flow = OAuth2WebServerFlow(
     client_id=CLIENT_ID, 
     client_secret=CLIENT_SECRET, 
     scope=SCOPE, 
     user_agent=USER_AGENT, 
    ) 
    return flow 


def get_people(): 
    """Return a people_service.""" 
    flow = make_flow() 
    storage = Storage('info.dat') 
    credentials = storage.get() 
    if credentials is None or credentials.invalid: 
     credentials = run_flow(flow, storage) 

    http = httplib2.Http() 
    http = credentials.authorize(http) 
    people_service = build(serviceName='people', version='v1', http=http) 
    return people_service 


def create_contact(people, user): 
    """Create a Google Contact.""" 
    request = people.createContact(
     body={ 
      'names': [{'givenName': user.name}], 
      'phoneNumbers': [ 
       {'canonicalForm': user.phone, 'value': user.phone}], 
     } 
    ) 
    return request.execute() 


def demo(): 
    """Demonstrate getting contacts from Google People.""" 
    people_service = get_people() 
    people = people_service.people() 
    connections = people.connections().list(
     resourceName='people/me', 
     personFields='names,emailAddresses,phoneNumbers', 
     pageSize=2000, 
    ) 
    result = connections.execute() 
    s = json.dumps(result) 
    # with open('contacts.json', 'w') as f: 
    #  f.write(s) 
    return s 


if __name__ == '__main__': 
    print(demo())