将Google People API与Cloud Functions for Firebase结合使用 [英] Using Google People API with Cloud Functions for Firebase

查看:98
本文介绍了将Google People API与Cloud Functions for Firebase结合使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Cloud Services for Firebase从Google People API获取联系人列表,但是我只是得到一个空对象作为响应.有什么想法吗?下面的Cloud Functions代码:

I'm trying to get a list of contacts from the Google People API with Cloud Functions for Firebase 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);
      });

    });
  });

});

在Firebase控制台日志中,成功消息与空JSON对象一起打印.似乎正在成功授权,所以不太确定发生了什么.任何帮助将不胜感激.

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.

推荐答案

尝试将api调用包装在Promise中.直到我将我的承诺包装好之前,我遇到了类似的问题.另外,在链接到此处的特定示例中,我需要使用带有promise的resolve部分的response.data而不是response.labels. console.log()将是您最好的朋友.

Try wrapping the api call in a Promise. I had a similar issue until I wrapped mine in a promise. Also in the specific example linked to here I needed to use response.data and not response.labels with the the resolve part of the promise. console.log() will be your best friend here.

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

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

这篇关于将Google People API与Cloud Functions for Firebase结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆