Skip to content

Instantly share code, notes, and snippets.

@aritraroy24
Created August 10, 2020 18:48
Show Gist options
  • Save aritraroy24/073e2b416d81228a23770bbfa77ab0b0 to your computer and use it in GitHub Desktop.
Save aritraroy24/073e2b416d81228a23770bbfa77ab0b0 to your computer and use it in GitHub Desktop.
Code to retrieve email and phone no from google contacts
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
import json
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/contacts.readonly']
def main():
"""Shows basic usage of the People API.
Prints the name of the first 10 connections.
"""
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('token.pickle'):
with open('token.pickle', '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(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('people', 'v1', credentials=creds)
# Call the People API
print('List of all name with email id and phone number:\n')
results = service.people().connections().list(
resourceName='people/me',
pageSize=1500,
personFields='names,emailAddresses,phoneNumbers').execute()
connections = results.get('connections', [])
i=1
for person in connections:
names = person.get('names', [])
emails = person.get('emailAddresses', [])
phones = person.get('phoneNumbers')
if names and emails:
name = names[0].get('displayName')
email = emails[0]['value']
phone = phones[0]['value']
print(f"\n{i}. {name} - {email} {phone}")
i+=1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment