I face error 403: Not Authorized to access this resource/api
when i try to access list the users in from the Directory API.
I've followed the steps here: https://developers.google.com/admin-sdk/directory/v1/guides/delegation
- The domain on google admin console has the same name that the organisation in google API
- The Admin SDK is enabled
- the service account is allowed in the API access manager
- The authentication works and I have a token
Here's the code I use
const google = require('googleapis').google;
const directory = google.admin('directory_v1');
const key = require('./creds.json');
const scopes = [ "https://www.googleapis.com/auth/admin.directory.user", "https://www.googleapis.com/auth/admin.directory.group", "https://www.googleapis.com/auth/admin.directory.orgunit"]
jwtOptions = {
email: key.client_email,
key: key.private_key,
scopes: scopes,
}
const jwtClient = new google.auth.JWT(jwtOptions)
jwtClient.authorize( (err, tokens) => {
if (err) {
console.log(err);
return;
}
// Make an authorized request to list Drive files.
directory.users.list({
auth: jwtClient,
domain: 'mydomain.com',
}, (err, resp) => {
if (err) {
console.log('error')
console.log(err.errors)
} else {
console.log('success')
}
});
});
What did I have missed?