people.connections.list没有使用Python客户端库返回联系人 [英] people.connections.list not returning contacts using Python Client Library
问题描述
我正在尝试使用Python客户端库以编程方式访问自己的个人Google帐户中的联系人列表
I'm trying to programmatically access the list of contacts on my own personal Google Account using the Python Client Library
这是一个无需用户输入即可在服务器上运行的脚本,因此我将其设置为使用我设置的服务帐户中的凭据.我的Google API控制台设置如下所示.
This is a script that will run on a server without user input, so I have it set up to use credentials from a Service Account I set up. My Google API console setup looks like this.
我正在使用以下基本脚本,这些脚本是从API文档中提供的示例中提取的-
I'm using the following basic script, pulled from the examples provided in the API docs -
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时,它将返回一个没有任何连接"键的结果集.具体来说,它返回-
When I hit the API it returns a result set without any 'connections' key. Specifically it returns -
>>> results
{u'nextSyncToken': u'CNP66PXjKhIBMRj-EioECAAQAQ'}
是否存在与我的设置或代码有关的错误信息?有没有办法查看响应HTTP状态代码或获得有关其尝试执行的操作的更多详细信息?
Is there something pertaining to my setup or code that's incorrect? Is there a way to see the response HTTP status code or get any further detail about what it's trying to do?
谢谢!
旁注:当我使用 试试看!" API文档中的功能,它可以正确返回我的联系人.尽管我怀疑是使用客户端库,而是依赖于通过OAuth的用户授权
Side note: When I try it using the "Try it!" feature in the API docs, it correctly returns my contacts. Although I doubt that uses the client library and instead relies on user authorization via OAuth
推荐答案
personFields 掩码是必需的.指定一个或多个有效路径.有效路径记录在 https://developers.google.com上/people/api/rest/v1/people.connections/list/.
The personFields mask is required. Specify one or more valid paths. Valid paths are documented at https://developers.google.com/people/api/rest/v1/people.connections/list/.
另外,使用 fields 掩码指定部分响应中包含哪些字段.
Additionally, use fields mask to specify which fields are included in a partial response.
代替:
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()
这篇关于people.connections.list没有使用Python客户端库返回联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!