2016-07-31 21 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())