-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfetchContact.py
96 lines (76 loc) · 2.98 KB
/
fetchContact.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from __future__ import print_function
import os.path
from typing_extensions import final
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
import datetime
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/calendar-python-quickstart.json
# SCOPES = 'https://www.googleapis.com/auth/calendar'
SCOPES = 'https://www.googleapis.com/auth/contacts.readonly'
CLIENT_SECRET_FILE = 'keys3.json'
APPLICATION_NAME = 'Birthday Wishing App'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
# credential_dir = os.path.join(home_dir, '.credentials4')
credential_dir = os.path.join(os.getcwd(), 'credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'calendar-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
# If modifying these scopes, delete the file
# token.json.
# SCOPES = ['https://www.googleapis.com/auth/contacts.readonly']
# def main():
def importContacts():
"""Shows basic usage of the People API.
Prints the name of the first 10 connections.
"""
creds = get_credentials()
http = creds.authorize(httplib2.Http())
# service = discovery.build('calendar', 'v3', http=http)
service = build('people', 'v1', credentials=creds)
# Call the People API
# print('List of Connection :')
results = service.people().connections().list(
resourceName='people/me',
pageSize=1000,
personFields='names,emailAddresses,birthdays,phoneNumbers').execute()
connections = results.get('connections', [])
finalpeople = []
for person in connections:
birthdays = person.get('birthdays', [])
if birthdays:
finalpeople.append(person)
return finalpeople