I am trying to create multiple contacts in my company using python and the people API.
I've researched it and found that I need to use people API to edit contacts via API but I'm not finding good examples of how this could be done.
I am using the following command to make a simple listing of my 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 urllib3
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/contacts']
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 10 connection names')
results = service.people().connections().list(
resourceName='people/me',
pageSize=10,
personFields='names,phoneNumbers').execute()
connections = results.get('connections', [])
for person in connections:
names = person.get('names', [])
phones = person.get('phoneNumbers', [])
if names and phones:
name = names[0].get('displayName')
phones = phones[0].get('canonicalForm')
print(name, phones)
if __name__ == '__main__':
main()
Output:
List 10 connection names
Eeverton None
Evetton None
Paulinha None
Wayne +5521992*****
Joao Pedro +55219643*****
Mae +552199*****
Maae +552199*****
Advogado Gb +5521964*****
working perfection.
But what I need is to create new contacts.
I changed the scopes
I authenticated oauth2
I have all files and keys properly stored (token.picke and credentials.json)
how should i go about creating new contacts? functions or POST?
Could you provide a simple code example for creating contacts?
https://developers.google.com/people/api/rest/v1/people/createContact
works with:
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
SCOPES = ['https://www.googleapis.com/auth/contacts']
def main():
"""Shows basic usage of the People API.
Prints the name of the first 10 connections.
"""
creds = None
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)
# CREATING CONTACTSSSSSSSS
service.people().createContact( body={
"names": [
{
"givenName": "Samkit"
}
],
"phoneNumbers": [
{
'value': "8600086024"
}
],
"emailAddresses": [
{
'value': '[email protected]'
}
]
}).execute()
if __name__ == '__main__':
main()