我正在尝试使用 Firebase 的 Cloud Functions 从 Google People API 获取联系人列表,但我只得到一个空对象作为响应。有什么想法吗?云函数代码如下:
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 对象一起打印。似乎授权成功,所以不太确定发生了什么。任何帮助将不胜感激。
尝试将 api 调用包装在 Promise 中。我也遇到过类似的问题,直到我做出承诺。另外,在链接到此处的具体示例中,我需要将response.data而不是response.labels与承诺的解析部分一起使用。 console.log() 将是你最好的朋友。
https://cloud.google.com/community/tutorials/cloud-functions-oauth-gmail https://cloud.google.com/community/tutorials/cloud-functions-oauth-gmail
https://github.com/GoogleCloudPlatform/community/blob/master/tutorials/cloud-functions-oauth-gmail/index.js https://github.com/GoogleCloudPlatform/community/blob/master/tutorials/cloud-functions-oauth-gmail/index.js
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)