簡體   English   中英

Firebase-NodeJS:帶有OAuth2的Google API的域范圍委派

[英]Firebase - NodeJS: Domain-Wide Delegation with OAuth2 for Google API

目標:

此Firebase Cloud Function應該使用具有域范圍委派的 Cloud API ,以便在Firebase數據庫更改時,任何用戶都可以更新一些 G Suite管理面板用戶信息。

題:

我應該使用哪種程序包方法來為我的應用程序獲取整個domian范圍的委托。

細節:

我並未將有關Google身份識別平台的點點滴滴連絡起來, 在這一步 ,我被困在這里。 Firebase托管的Node.js應用程序如何針對Google API請求將網絡和訪問令牌組合在一起?

Firebase項目使用Google Cloud Platform項目,因此

我有...

  1. 通過GCP控制台中的API /憑據將service account actor添加到項目中
  2. 選中啟用G Suite域范圍委派
  3. 存儲了private_key.JSON。
  4. 具有服務帳戶客戶端ID的授權API客戶端(在G Suite管理面板中)

我應該使用...

Firebase :可能要查看Google OAuth2設置白名單區域 ,和/或使用從firebase獲得services.json

通過googleapis的 Google API :即使我使用firebase.auth.GoogleAuthProvider()來驗證用戶,也可以使用google.auth.OAuth2從GCP(例如應用程序或計算引擎)獲取域范圍的委托

通過google-auth-library進行的 Google Auth :同樣,即使我使用firebase.auth.GoogleAuthProvider()驗證用戶,也可以使用new GoogleAuth()從GCP(例如應用程序或計算引擎)獲取域范圍的委托

更新

我學過:

  1. Google的npm軟件包googleapis不適用於客戶端(瀏覽器)。 我現在正在嘗試在Firebase Cloud Functions中使用它

答案是:

使用googleapis通過JWT服務令牌進行服務帳戶身份驗證。

以下代碼已部署到Firebase Cloud Functions,並且日志似乎表明身份驗證已成功。

Firebase功能日志

  1. 3:56:35.101 PM授權開始執行功能
  2. 3:56:35.620 PM授權成功連接!
  3. 3:56:35.668 PM授權功能執行耗時568毫秒,狀態碼為200

NodeJS代碼

// Firebase Admin SDK
const functions = require('firebase-functions')
const admin = require('firebase-admin')
admin.initializeApp(functions.config().firebase)

// Google APIs
const googleapis = require('googleapis')
const drive = googleapis.drive('v3')
const gsuiteAdmin = googleapis.admin('directory_v1')

// Service Account Key - JSON
let privatekey = require("./privatekey.json")

let jwtClient = new googleapis.auth.JWT(
    privatekey.client_email,
    null,
    privatekey.private_key,
    ['https://www.googleapis.com/auth/drive',
        'https://www.googleapis.com/auth/admin.directory.user'])

// Firebase Cloud Functions - REST
exports.authorize = functions.https.onRequest((request, response) => {
    //authenticate request
    jwtClient.authorize(function (err, tokens) {
        if (err) {
            console.log(err)
            return
        } else {
            console.log("Successfully connected!")
        }
        response.send("Successfully connected!")
    })
})

暫無
暫無

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

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