簡體   English   中英

將 Firebase callablle function 上下文身份驗證與其他 Google API 一起使用

[英]Use Firebase callablle function context auth with other Google APIs

我有一個可調用的 function,我想在其中訪問表格 API,但不是使用服務帳戶,我想冒充用戶。 問題是我不想向客戶端發送授權 URL。 用戶已經在客戶端使用 google 登錄到 firebase 應用程序,並已同意我需要的所有范圍(包括表格 API 范圍),所以我想知道是否可以在上下文中使用 auth ZA8CFDE6331C4BEB26 參數一個可調用的 function 代表用戶與其他 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 過程。

謝謝!

我想到了! 正確的做法是將客戶端的訪問令牌作為參數發送到 function 並使用它代表用戶訪問 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 的訪問令牌。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM