I’m trying to get all the Project Access Manager Users (the users in the screen-shot) of the project I’m working on for a client. I’m doing it programmatically by making a get request with the generated public key and private key as the username and password for the auth header of the request.
Here’s my code:
const getDbProjectManagerUsers = async () => {
try {
const { MONGODB_PROJECT_PUBLIC_KEY, MONGODB_PROJECT_PRIVATE_KEY, MONGODB_PROJECT_ID } = process.env;
const url = `https://cloud.mongodb.com/api/atlas/v2/groups/${MONGODB_PROJECT_ID}/databaseUsers`;
const url2 = `https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/databaseUsers`
const auth = {
username: MONGODB_PROJECT_PUBLIC_KEY,
password: MONGODB_PROJECT_PRIVATE_KEY,
};
console.log("auth: ", auth)
console.log('url: ', url)
const response = await axios.get(url, {
auth,
headers: {
Accept: 'application/vnd.atlas.2023-02-01+json'
}
})
if (response.status !== 200) {
throw new Error('Error getting project manager users from the db')
}
console.log('response.data: ', response.data)
} catch (error) {
console.error('An error has occurred getting the project managers users from the db: ', error);
}
}
The response that I’m getting is a 401 with the following error message: ‘You are not authorized for this resource.’
I’ve checked the logs; I’m not getting undefined for any of my environment variables.
This doesn’t make sense to me because the public-private key pair that was generated has a ‘Project Read Only’ role, which is necessary to send the request to the API, as mentioned in the docs linked here: MongoDB Atlas Administration API.
What am I missing? Thanks for any responses.