你好,开发社区。我正在尝试调试firebase函数,并尝试使用一些教程,但没有成功...
我尝试过(https://medium.com/@mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36)(https://medium.com/@david_mccoy/build-and-debug-firebase-functions-in-vscode-73efb76166cf)
我的目的是与Google联系。
const { google } = require('googleapis');
const oauthUserCredential = require('./oauthUserCredential.json')
const OAuth2 = google.auth.OAuth2
const key = require('./serviceAccountKey.json')
const jwt = new google.auth.JWT(key.client_email, null, key.private_key, 'https://www.googleapis.com/auth/contacts')
exports.getGoogleContacts = functions.https.onCall(async (data, context) => {
const requestingUser = data.requestingUser
console.log('getGoogleContacts-requestingUser', requestingUser)
const oauth2Client = new google.auth.OAuth2(
'client_id',
'client_secret',
'http://localhost:5000/xxx-xxx/us-central1/OAuthCallbackUrl'
);
const contacts = google.people({
version: 'v1',
auth: oauth2Client,
});
console.log('contacts ?', contacts)
(async () => {
const { data: groups } = await contacts.people.get({
resourceName: 'contactGroups',
});
console.log('Contact Groups:\n', groups);
})()
jwt.authorize((err, response) => {
console.log('inside authorize')
if (err) {
console.error(err);
response.end();
return;
}
// Make an authorized request to list contacts.
contacts.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);
});
});
// this is another approach I´ve tried, but it´s also not working
const oAuth2Client = new OAuth2(
oauthUserCredential.web.client_id,
oauthUserCredential.web.client_secret,
oauthUserCredential.web.redirect_uris,
)
oAuth2Client.setCredentials({
refresh_token: oauthUserCredential.refresh_token
})
return new Promise((resolve, reject) => {
console.log('[INSIDE PEOPLE CONNECTIONS]')
contacts.people.connections.list({
auth: oauth2Client //authetication object generated in step-3
}, function (err, response) {
if (err) {
console.log('contacts.people.connections error')
console.log(err)
reject(new Error(err))
} else if (response) {
console.log('contacts.people.connections response')
console.log(response)
resolve(response)
}
});
})
.then(result => { return { found: result } })
.catch(err => { return { error: err } })
})
我尝试了几种不同的方法并遵循了不同的教程(Using Google People API with Cloud Functions for Firebase)(https://flaviocopes.com/google-api-authentication/)(https://medium.com/@smccartney09/integrating-firebase-cloud-functions-with-google-calendar-api-9a5ac042e869)(https://cloud.google.com/community/tutorials/cloud-functions-oauth-gmail)
但是他们都没有清楚显示我如何获取我的联系人列表。通过遵循本教程,我可以使用客户端代码(https://labs.magnet.me/nerds/2015/05/11/importing-google-contacts-with-javascript.html)但是我认为将暴露在客户端的client_id,client_secret和apiKey放在安全位置会是一个安全问题...
我也在提交教程请求,以使其非常清楚如何使用Firebase函数从Google帐户获取联系人列表。
您收到的错误是因为云函数无法找到名为xxxx的函数来执行,因为您尚未在index.js文件中定义任何名为xxxx的函数。
您的云函数要执行的名称,根据错误消息是xxxx,但是您在index.js中调用的函数是getGoogleContacts。请确保这些名称相同,例如将getGoogleContacts更改为xxxx或将要执行的函数更改为getGoogleContacts