metalaureate
metalaureate

Reputation: 7732

Can't get Google Contacts Data with accessToken

I'm successfully getting the access token with scope (https://www.googleapis.com/auth/contacts.readonly) to read contacts from Google Contacts API, but now I'm stumped with how to request the contacts of the credentialized user.

I'm using https://github.com/jaredhanson/passport-google-oauth to get token.

Here is the Google API doc: https://developers.google.com/google-apps/contacts/v3/#retrieving_all_contacts

I tried

GET https://www.google.com/m8/feeds/contacts/default/full?accessToken=<my access token>

But that 401s.

I'm missing something big...

Upvotes: 0

Views: 499

Answers (1)

metalaureate
metalaureate

Reputation: 7732

'It doesn't appear documented anywhere, but its access_token not accessToken.

Here's my example:

app.get('/auth/google/callback', function (req, res, next) {
    passport.authenticate('google', function (err, user, info) {
        request.get("https://www.google.com/m8/feeds/contacts/default/full?v=3.0&access_token=" + user.accessToken, function (error, result) {
            var xml = result.body;
            var parseString = require('xml2js').parseString;
            parseString(xml, function (err, result) {
                var entries = result.feed.entry, contacts = [];
                _.each(entries, function (entry) {
                    if (!(entry['gd:name']===undefined)) {
                        var gdName = entry['gd:name'][0]['gd:fullName'][0];
                        var gdEmail = entry['gd:email'][0]['$']['address'];
                        contacts.push({name: gdName, email: gdEmail});
                    }

                });
                res.send(contacts);
            });

        });
    })(req, res, next)
});

Upvotes: 1

Related Questions