I spent a lot of days to find way to authentificate to google admin api with service account. Now I have new error message:
Error: native promise missing, set fetch.Promise to your favorite alternative
Here my code:
require('../global')
const fs = require('fs');
const {google} = require('googleapis');
const KEYFILE = global.service_account;
const SCOPES = global.scopes;
(async function run() {
async function readPrivateKey() {
const content = fs.readFileSync(KEYFILE);
return JSON.parse(content.toString());
}
async function authenticate(key) {
const jwtClient = new google.auth.JWT(
key.client_email,
null,
key.private_key,
SCOPES
);
await jwtClient.authorize();
return jwtClient;
}
async function createEvent(auth) {
let admin = google.admin({ version: 'directory_v1' });
await admin.users.list({
customer:'my_customer',
maxResults: 500
});
}
// MAIN
try {
const key = await readPrivateKey();
console.log(key)
const auth = await authenticate(key);
await createEvent(auth);
} catch (e) {
console.log('Error: ' + e);
}
})();