from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request from apiclient import discovery import httplib2 import os # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/contacts'] def main(): pathJson = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\credentials.json" pathPickle = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\token.pickle" creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists(pathPickle): with open(pathPickle, 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( pathJson, SCOPES) creds = flow.run_local_server(port=0) with open(pathPickle, 'wb') as token: pickle.dump(creds, token) service = build('people', 'v1', credentials=creds) print('List 10 connection names') results = service.people().connections().list( resourceName='people/me', pageSize=100, personFields='names,emailAddresses').execute() connections = results.get('connections', []) for person in connections: print(person) names = person.get('names', []) if names: name = names[0].get('displayName') print(name) if __name__ == '__main__': main()