Export Google Contacts using Node.js and People API

Meher Ullah Khan Raj
3 min readNov 23, 2020
https://raw.githubusercontent.com/raj-khan/nodejs-export-google-contacts/master/export-contacts.png

Google Contacts API allows client applications to play with the user’s contacts. Contacts are stored in the user’s Google Account. To follow the article you can export your Google contacts into a JSON file.

Prerequisites

To run this quickstart, you need the following prerequisites:

  • Node.js & npm installed.
  • A Google account

So let’s start step by step:-

Complete the steps described in the rest of these steps to create a simple Node.js command-line application that makes contacts export requests to the People API.

Step 1: Turn on the People API

Click Enable the People API button to create a new Cloud Platform project and automatically enable the People API. or simply follow the steps:-

  1. Enter a new project name or use the default name, I prefer the default name;
  2. Click Next;
  3. Select Web server or Desktop App (Prefer: Web Server)
  4. Pass a redirect URL;
  5. In the resulting dialog click DOWNLOAD CLIENT CONFIGURATION and save the file credentials.json to your working directory.

Step 2: Install the client library

Run the following commands to install the libraries using npm:

npm install googleapis@39 --save

Step 3: Set up the sample

Create a file named main.js in your working directory and copy in the following code:

Step 4: Change your credentials.json file

Go to credentials.json file see the initial line of this file, replace web by installed or you will get an error.

Step 5: Run the sample

Run the sample using the following command:

node main.js

The first time you run the sample, it will prompt you to authorize access:

  1. Browse to the provided URL in your web browser.

If you are not already logged into your Google account, you will be prompted to log in. If you are logged into multiple Google accounts, you will be asked to select one account to use for authorization.

If you don’t have a browser on the machine running the code, and you’ve selected “Desktop app” when creating the OAuth client, you can browse to the URL provided on another machine, and then copy the authorization code back to the running sample.

2. Click the Accept/Allow button.

3. Copy the code you’re given, paste it into the command-line prompt, and press Enter.

If you do not see any Code then check the browser URL where you can see a parameter called code=’something’ copy the code before & sign, because that was another parameter.

After this, The file token.json stores the user’s access and refresh tokens, and is created automatically when the authorization flow completes for the first time. Load previously authorized token from a file, if it exists.

4. Paste the code where asked in your terminal. You will see a result printed in your terminal which is Contacts Name & Emails for 10 contacts by default.

5. Now copy the main.js code into your index.js file.

6. You’re all set, enjoy 🚀

Conclusion

In this article, I just export all contacts, If you need you can do more things like the update, delete user contacts. You will find the Google API Client for Node.js documentation in the further reading section.

In People API you can use your preferred languages:-
Supported Languages:
→ Java
→ PHP
→ Node.js
→ Python
→ Ruby

Further reading

--

--