Использование Google People API с облачными функциями для Firebase

Я пытаюсь получить список контактов из Google People API с облачными функциями для Firebase, но в качестве ответа я получаю только пустой объект. Какие-нибудь мысли? Код облачных функций ниже:

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);
      });

    });
  });

});

В журналах консоли Firebase сообщение об успехе печатается вместе с пустым объектом JSON. Кажется, что авторизация успешно, поэтому не совсем уверен, что происходит. Любая помощь будет принята с благодарностью.

0 ответов

Попробуйте обернуть вызов API в обещание. У меня была похожая проблема, пока я не завернул свое обещание. Также в конкретном примере, связанном с здесь, мне нужно было использовать response.data, а не response.labels с частью разрешения обещания. console.log() будет вашим лучшим другом здесь.

https://cloud.google.com/community/tutorials/cloud-functions-oauth-gmail

https://github.com/GoogleCloudPlatform/community/blob/master/tutorials/cloud-functions-oauth-gmail/index.js

Другие вопросы по тегам