2016-07-31 5 views
11

मैं प्रोग्राम के रूप में अजगर क्लाइंट लाइब्रेरी का उपयोग अपने स्वयं के व्यक्तिगत Google खाते पर संपर्कों की सूची का उपयोग करने की कोशिश कर रहा हूँ अजगर क्लाइंट लाइब्रेरी का उपयोग करके संपर्क वापस नहींpeople.connections.list

यह एक स्क्रिप्ट है कि एक पर चलेंगे है उपयोगकर्ता इनपुट के बिना सर्वर, इसलिए मैंने इसे स्थापित सेवा खाते से प्रमाण-पत्रों का उपयोग करने के लिए सेट अप किया है। मेरा Google एपीआई कंसोल सेटअप इस तरह दिखता है।

enter image description here

मैं निम्न बुनियादी स्क्रिप्ट का उपयोग कर रहा हूँ, एपीआई डॉक्स में दिए गए उदाहरणों से खींचा -

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! 

जब मैं एपीआई मारा यह एक परिणाम के किसी भी 'कनेक्शन' के बिना सेट रिटर्न कुंजी। विशेष रूप से यह लौटाता है -

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

क्या मेरे सेटअप या कोड से संबंधित कुछ गलत है? क्या प्रतिक्रिया HTTP स्थिति कोड देखने का कोई तरीका है या इसके बारे में कोई और जानकारी प्राप्त करने का प्रयास कर रहा है?

धन्यवाद!

साइड नोट: जब मैं the "Try it!" feature in the API docs का उपयोग करके इसे आजमाता हूं, तो यह मेरे संपर्कों को सही ढंग से देता है। हालांकि मुझे संदेह है कि क्लाइंट लाइब्रेरी का उपयोग करता है और इसके बजाय ओएथ

+0

अरे, मेरे पास बिल्कुल वही समस्या है। क्या आप इसे हल करने में सक्षम थे? धन्यवाद। – katericata

+0

@katericata - मैंने नहीं किया, क्षमा करें :( – user2490003

उत्तर

5

व्यक्ति फ़ील्ड मास्क की आवश्यकता है के माध्यम से उपयोगकर्ता प्रमाणीकरण पर निर्भर करता है। एक या अधिक मान्य पथ निर्दिष्ट करें। वैध पथ 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

यहां एक कामकाजी डेमो है। मैंने अभी अभी इसका परीक्षण किया है। अजगर 3.5.2

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

आप demo.py में सहेज सकते हैं और फिर बस इसे चलाते हैं। यदि आप इसका उपयोग करना चाहते हैं और API उपयोग पर एक और उदाहरण है तो मैंने create_contact फ़ंक्शन छोड़ा।

CLIENT_ID और CLIENT_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())