Though Guido van Rossum has created python as a hobby project in the 90’s decade, it has become the most popular language now. Due to its ability to work in various fields and easy syntax, python is now used in Big-Data Analysis, ML & AI, Deep-learning, image-processing, as well as in web development, game development, and so on. Moreover, it has plenty of libraries that one can use to easily and quickly complete the task.
Receiving the phone no and email id can be obtained from Google contact by using their API service. To access one’s google contact, we need that particular user’s permission via the API access level. Here ‘Gmail API’ comes to rescue us easily. Though Google provides two more API, aka Contact API version 3.0(stopped working from June 15, 2021 ) and People API, from my point of view, the ‘Gmail API’ seems to be a lot easier to do the job.
We’ve used python to fetch the phone no and email id from Google contacts. For this, the followings are required —
Step 1: Turning on the Gmail API
 Go to the link below to enable the Gmail API- Link to enable Gmail API
Figure 1: Turning on the Gmail API
 Then, one will need to provide a project name and click on the ‘Next’ button.
Figure 2: Provide a project name
 The next step is configuring the OAuth client. If one wants to retrieve the data on his computer, he’ll need to set it as the ‘Desktop app’. Otherwise, one can choose his choice according to his requirement and click on the ‘Create’ button.
Figure 3: Configure the OAuth client
 The final step of ‘Enabling Gmail API’ is to download the ‘Client Configuration’. Note that one has to download the ‘Client Configuration’ specifically in the project folder to enable the API successfully.
Figure 4: Download the client configuration
Step 2: Installing the Google Client Library
We have to use/install Google Client Library python library to access it. To install it via pip —
Figure 5: Installing the Google Client Library
Step 3: Setting up with code
Sample code given by Google Client
Please note that the project name(given at the time of enabling Gmail API) and the python file name should be the same. We’ve run the python file from ‘Windows PowerShell’ —
Figure 6: Running the python file from ‘Windows Powershell’
It’ll open a localhost browser window for accessing your Google account(from where the contacts will be retrieved), and the following steps are —
Figure 7: Steps to access the required Google account
[a] Choosing one Gmail account from where the contacts will be retrieved
[b] There’ll be an alert message saying ‘This app isn’t verified’. The ‘Advanced’ option should be clicked
[c] After that ‘Go to Quickstart(unsafe)’ option should be clicked for further proceeding. Note that the project name will replace this ‘Quickstart’ word if one chooses a different project name while Enabling the Gmail API (step 1).
[d] The ‘Allow’ option should be selected for permitting our application.
[e] Again, the ‘Allow’ option should be selected for allowing our application to see and download our contacts from Google Contacts.
[f] After completing all the previous steps, a message saying ‘The authentication flow has completed. You may close this window.’ It should be shown in the browser window. The browser window can be closed now.
After closing the window, if we take a look in the Windows PowerShell, we’ll be able to see that our program has finished all the tasks giving us a message like the following —
Figure 8: Powershell message after completing the authentication flow
Now, if we run the specific python file from Windows PowerShell, it’ll give a result like below —
Figure 9: Fetched different folder and contact headings printed on the Powershell screen
Note that it has fetched different folder headings and contact headings only while using the sample code given by the ‘Gmail API Guide’.
Code to retrieve Email and Phone No.
All the steps up to ‘authentication,’ i.e. previously described process 1, must be performed. After that, if one runs the above code, that’ll give a result like the following —
Figure 10: Fetched names, email ids, phone numbers associated with that specific Google account
Note that, All the names, email ids, and phone numbers have been retrieved from ‘Google Contacts’ associated with that Google account.
- • One can’t fetch all the emails and phone numbers from Google Contacts if he doesn’t take permission from the user via the API access level.
- • It is an easy way to retrieve all the email ids and phone numbers from ‘Google contacts’ without ‘Django’.
- • One can manipulate the results according to his choice whether to fetch only name-email id or name-phone number or name-both or anything by changing as per his requirement.
- • The output can be used in any further work, such as sending mail using a voice assistant or making a phone call.
- • The ‘Client Configuration’ will be downloaded as credentials.json
- • All the details will be saved in the token.pickle file, and no further retrieving will be occurred if there is no change.
- • All the files should be in the same folder path for the process
- • This project has been done in windows 10. But one can further take it forward for Linux and iOS.
⁕ ⁕ ⁕
Thank you for reading.
I hope you found this “Retrieving Email and Phone No. for a Desktop App from Google Contacts using Python and Gmail API” article helpful. Please share if you like and leave any comment to let me know your thoughts.