у кого-нибудь есть пример кода о том, как получить доступ к Google Directory API из облачной функции Google? В частности, я хочу использовать токен обновления 3LO для создания токена доступа, который авторизован для доступа к API каталога, возможно, с использованием библиотеки googleapis.
Как авторизовать доступ к Directory API в Google Cloud Function
Ответы (1)
Поскольку никто не ответил, я углубился в некоторые документы и нашел эту страницу, в котором описывается, как получить токен обновления, предоставленный извне приложения (curl + браузер), и подключить его к коду для вызова 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);
}
}
});
person
Michael
schedule
13.04.2017