Friday, 21 April 2017

Using Google People API with Firebase Cloud Functions

I'm trying to get a list of contacts from the Google People API with Firebase Cloud Functions but I'm only getting an empty object as the response. Any thoughts? Cloud Functions code below:

var functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

var google = require('googleapis');
var people = google.people('v1');

exports.contacts = functions.https.onRequest((request, response) => {
  admin.database().ref('/settings/contacts/credentials/serviceAccount').once("value", function(data) {
    var authClient = new google.auth.JWT(
      data.child('clientEmail').val(),
      null,
      data.child('privateKey').val(),
      ['https://www.googleapis.com/auth/contacts'],
      null
    );

    authClient.authorize(function (err, tokens) {
      if (err) {
        console.error(err);
        response.end();
        return;
      }

      // Make an authorized request to list contacts.
      people.people.connections.list({auth: authClient, resourceName: 'people/me'}, function(err, resp) {
        if (err) {
          console.error(err);
          response.end();
          return;
        }

        console.log("Success");
        console.log(resp);
        response.send(resp);
      });

    });
  });

});

In the Firebase console logs, the success message is printed along with the empty JSON object. Seems to be authorizing successfully so not quite sure what's going on. Any help would be greatly appreciated.



via Aaron Wilson

No comments:

Post a Comment