将 Firebase callablle 函数上下文身份验证与其他 Google API 结合使用



我有一个可调用的函数,我想在其中访问Sheets API,但不要使用服务帐户,我想模拟用户。问题是我不想向客户端发送授权 URL。用户已经在客户端使用 google 登录了 firebase 应用,并同意了我需要的所有范围(包括表格 API 范围(的权限,所以我想知道是否可以在可调用函数的上下文参数中使用 auth 对象来代表用户向其他 API 进行身份验证。

function exportSpreadsheets(data, context)
{
const {google} = require('googleapis');
//How do I create an OAuth2 object I can use to access the API
//without having to send the user an authentication link?
//Maybe using the data in context.auth.token?
const auth = new google.auth.OAuth2();
const sheets = google.sheets({version: 'v4', auth});
sheets.spreadsheets.create()
.then(x =>
{
console.log(x);
});
}

我尝试了上面的代码,但它不起作用。我正在努力理解所有OAuth2过程。

谢谢!

我想通了! 执行此操作的正确方法是将客户端的访问令牌作为参数发送到函数,并使用它来代表用户访问 API。喜欢这个:

function exportSpreadsheet(data, context)
{
const oauth = new google.auth.OAuth2({
clientId: '<your-apps-client-id>',
clientSecret: '<your-apps-client-secret>',
});
oauth.setCredentials(data);
const sheets = google.sheets({version: 'v4', auth: oauth});
//Use the API

此外,您必须发送提供商的访问令牌(在本例中为Google(,而不是Firebase的访问令牌。

最新更新