CanciuCostin
CanciuCostin

Reputation: 1908

Getting contacts from Google api in node

I want to get the contacts using the google contacts api in nodejs, but there isn't any quickstart on the developer.google page for nodejs. I have found this wrapper on github https://github.com/hamdipro/google-contacts-api but I don't understand it and I don't know how to use it.

Can anyone tell me what can I do?

Upvotes: 0

Views: 4191

Answers (2)

Abdallah Absi
Abdallah Absi

Reputation: 91

Unfortunately, Google's official API for NodeJS doesn't support Contacts API. They instead use the People API. If you need to access "Other Contacts", you will need Contacts API.

You can still connect with Contacts API using the official googleapis library if you're already using it for other purposes by sending a request to the Contacts API after creating the auth client.

Given that you already have the access token of the user (e.g. if you generated it using Passport, here's the code:

const {google} = require("googleapis");
const authObj = new google.auth.OAuth2({
    access_type: 'offline',
    clientId: process.env.GOOGLE_ID,
    clientSecret: process.env.GOOGLE_SECRET,
});

Refresh access token automatically before it expires

authObj.on('tokens', (tokens) => {
    const access_token = tokens.access_token
    if (tokens.refresh_token){
        this.myTokens.refreshToken = tokens.refresh_token
        // save refresh token in the database if it exists
    }
        this.myTokens.accessToken = tokens.access_token       
        // save new access token (tokens.access_token)
}
authObj.setCredentials({
    access_token:this.myTokens.accessToken,
    refresh_token:this.myTokens.refreshToken,
});

Make the request to Contacts API:

authObj.request({
    headers:{
        "GData-Version":3.0
    },
    params:{
        "alt":"json",
        //"q":"OPTIONAL SEARCH QUERY",
        //"startindex":0
        "orderby":"lastmodified",
        "sortorder":"descending",
    },
    url: "https://www.google.com/m8/feeds/contacts/default/full"
}).then( response => {
    console.log(response); // extracted contacts
});

Upvotes: 2

vinit payal
vinit payal

Reputation: 1281

First thing instead of going with non-official package mentioned in question you should prefer using official package as they are well maintained, every under the hood changes are handled properly and also issues created are taken into considerations.

Official package for same is here.

Now steps to use above package to get contacts of a user :-

  1. Include googleapis using npm install googleapis --save
  2. Create a service client

    • var google = require('googleapis');
    • var contacts = google.people('v1');
  3. Authorise client to make request {Link for authentication docs}

  4. Making authenticated requests

    contacts.people.connections.list({ auth: oauth2Client //authetication object generated in step-3 }, function (err, response) { // handle err and response });

That should be enough to get user's contact data. Also for authentication if you are using this for domain apart from gmail and have admin access you can get all user's contacts using domain wide delegation otherwise you will have to manually allow access for each user.

Hope it helps. Let me know in comments if have any queries.

Upvotes: 0

Related Questions