NodeJS示例 – Firebase云function – 实例化Admin SDK目录服务对象

目标

在Firebase云端函数中使用googleapis获取我的G Suite域中所有用户的列表。

如何实例化一个Admin SDK目录服务对象 。 我没有看到一个NodeJS的例子,我不清楚如何设置和使用googleapis的请求。

上下文

此代码从Firebase云端函数运行,似乎可以进行身份​​validation。 现在,我如何在//TODO中的以下代码中设置服务对象:

 // 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!") } // TODO // USE SERVICE OBJECT HERE?? // WHAT DOES IT LOOK LIKE? response.send("Successfully connected!") }) }) 

操作顺序:

  1. 在Google云端控制台中创build服务帐户凭据
  2. 将域范围委派添加到服务帐户
  3. 授权G Suite – 安全 – 高级中的API
  4. 返回服务帐户并下载.json密钥文件

我很快就下载了.json密钥文件,例如,在授权G Suite中的API之前。 订单:使用DwD设置​​服务帐户, 然后在G Suite API中授权API, 然后下载.json密钥文件非常重要。

这个例子

 // 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 directory = googleapis.admin('directory_v1') // Service Account Key - JSON let privatekey = require("./privatekey.json") let impersonator = 'example@example.com' let jwtClient = new googleapis.auth.JWT( privatekey.client_email, null, // not using path option privatekey.private_key, ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/admin.directory.user.readonly'], impersonator ) // Firebase Cloud Functions - REST exports.getUsers = functions.https.onRequest((request, response) => { //authenticate request jwtClient.authorize(function (err, tokens) { if (err) { console.log(err) return } else { console.log("Successfully connected!") } //Google Drive API directory.users.list ({ auth: jwtClient, domain: 'example.com', maxResults: 10, orderBy: 'email', viewType: 'domain_public' }, function(err, res) { if (err) { console.log('The API returned an error: ' + err) return; } var users = res.users; if (users.length == 0) { console.log('No users in the domain.'); } else { console.log('Users:'); for (var i = 0; i < users.length; i++) { var user = users[i]; console.log('%s (%s)', user.primaryEmail, user.name.fullName) } response.send(users) } }) }) }) 

UPDATE

上面的例子是不安全的。 云端function(特别是G Suite域范围授权)不应回应http请求,除非它们来自您的应用程序。 在本例中 ,请参阅Cloud Function使用admin.auth().verifyIdToken(idToken)...来validation请求是否已通过Firebase进行身份validation。

如果您无法正确处理您的G Suite DwD云端function, 您可能会将G Suite API暴露给公众。