processaAuthApi.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. from __future__ import print_function
  2. import pickle
  3. import os.path
  4. from googleapiclient.discovery import build
  5. from google_auth_oauthlib.flow import InstalledAppFlow
  6. from google.auth.transport.requests import Request
  7. from apiclient import discovery
  8. import httplib2
  9. import os
  10. # If modifying these scopes, delete the file token.pickle.
  11. SCOPES = ['https://www.googleapis.com/auth/contacts']
  12. def main():
  13. pathJson = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\credentials.json"
  14. pathPickle = "C:\\Users\\chris\\OneDrive\\Documentos\\GitHub\\MOBEES-BR\\Backend\\dsp\\adm\\auth\\token.pickle"
  15. creds = None
  16. # The file token.pickle stores the user's access and refresh tokens, and is
  17. # created automatically when the authorization flow completes for the first
  18. # time.
  19. if os.path.exists(pathPickle):
  20. with open(pathPickle, 'rb') as token:
  21. creds = pickle.load(token)
  22. # If there are no (valid) credentials available, let the user log in.
  23. if not creds or not creds.valid:
  24. if creds and creds.expired and creds.refresh_token:
  25. creds.refresh(Request())
  26. else:
  27. flow = InstalledAppFlow.from_client_secrets_file(
  28. pathJson, SCOPES)
  29. creds = flow.run_local_server(port=0)
  30. with open(pathPickle, 'wb') as token:
  31. pickle.dump(creds, token)
  32. service = build('people', 'v1', credentials=creds)
  33. print('List 10 connection names')
  34. results = service.people().connections().list(
  35. resourceName='people/me',
  36. pageSize=100,
  37. personFields='names,emailAddresses').execute()
  38. connections = results.get('connections', [])
  39. for person in connections:
  40. print(person)
  41. names = person.get('names', [])
  42. if names:
  43. name = names[0].get('displayName')
  44. print(name)
  45. if __name__ == '__main__':
  46. main()