Firebase – NodeJS:用于Google API的OAuth2域范围委派

目标:

此Firebase云端function应使用Cloud API与域范围委派,以便任何用户在Firebase数据库更改时更新一些 G Suitepipe理控制台用户信息。

题:

我应该使用哪个包的方法来获得应用程序的广泛代理权。

  • firebase : firebase.auth.GoogleAuthProvider()
  • googleapis : google.auth.OAuth2
  • google-auth-library : new GoogleAuth()

细节:

关于Google Identity Platform ,我并没有把这个问题连接起来,而我却陷入了这一步 。 firebase托pipe的nodejs应用如何将Google API请求的Web和访问令牌放在一起?

Firebase项目使用Google云端平台项目

我有…

  1. 通过GCP-Console中的API /凭证添加到项目中的service account actor
  2. 选中启用G Suite域范围委派
  3. 存储private_key.JSON。
  4. 授权API客户端(位于G Suitepipe理控制台中)和服务帐户客户端ID

我应该使用…

Firebase :可能会查看Google OAuth2设置的白名单区域 ,和/或使用我从firebase获得的services.json 。

Google API通过googleapis :尽pipe我使用firebase.auth.GoogleAuthProvider()来validation用户,也许可以使用google.auth.OAuth2从GCP获取域范围的委派(如应用程序或计算引擎)

Google Auth通过google-auth-library :同样,即使我使用firebase.auth.GoogleAuthProvider()来validation用户,也许使用new GoogleAuth()从GCP获取域范围的委托(如应用程序或计算引擎)

更新

我学过:

  1. Google的npm包googleapis不适用于客户端(浏览器)。 我现在正尝试在Firebase云端函数中使用它

答案是:

使用googleapis进行与JWT服务令牌的服务帐户validation。

下面的代码部署到Firebase云端函数,日志似乎表示authentication成功。

Firebasefunction日志

  1. 3:56:35.101 PM授权开始执行function
  2. 3:56:35.620 PM授权成功连接!
  3. 3:56:35.668 PM授权function执行耗时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!") }) })