如何授权访问Google云端函数中的目录API

有没有人有一个关于如何从谷歌云function访问Google的目录API的代码示例? 更具体地说,我想使用3LO刷新令牌来生成访问令牌,该令牌有权访问目录API,可能使用“googleapis”库。

       

网上收集的解决方案 "如何授权访问Google云端函数中的目录API"

由于没有人回应,我深入挖掘了一些文档,发现这个页面描述了如何从应用程序(curl + browser)外部获取刷新令牌,并将其插入到代码中以调用目录API:

const google = require('googleapis'); const admin = google.admin('directory_v1'); const OAuth2 = google.auth.OAuth2; var oauth2Client = new OAuth2( 'client_id', 'client_secret', 'redirect_url' ); //Store a refresh token from outside Cloud Functions oauth2Client.setCredentials({ refresh_token: 'refresh_token_from_curl' }); //Make sure the access_token is fresh (they expire every hour-ish) oauth2Client.refreshAccessToken(function(err,tokens){ }); admin.tokens.list({ auth: oauth2Client, userKey: userEmail }, function(err,response){ if (err) { console.log('The Directory API returned an error: ' + err); return reject(err); } var tokens = response.items; if(tokens == null){ console.log('No tokens for ' + userEmail); } else { console.log('Tokens:'); for (var i = 0; i < tokens.length; i++){ var token = tokens[i]; console.log('clientId: ' + token.clientId); console.log('displayText: ' + token.displayText); console.log('anonymous: ' + token.anonymous); console.log('nativeApp: ' + token.nativeApp); console.log('userKey: ' + token.userKey); console.log('scopes: ' + token.scopes); } } });