i have successfully created the Mobile Device Management application with the help of GCP Console project using Node.js. Now, I want to grant access to the role when third-party users want to join MDM application.
Here is the code I have tried:
const { google } = require('googleapis');
const auth = require('./auth'); // Assuming you have auth setup properly
const iam = google.iam('v1');
// Function to grant IAM roles to users
const grantIAMRoles = async (projectId, userEmails) => {
const resource = {
resource: {
policy: {
bindings: [
{
role: 'roles/editor',
members: userEmails.map(email => `user:${email}`) // Assigning 'Editor' role
},
{
role: 'roles/androidmanagement.user',
members: userEmails.map(email => `user:${email}`) // Assigning 'Android Management User' role
}
// Add more roles if needed
]
}
},
auth: auth // Assuming you have authentication set up properly
};
try {
const response = await iam.projects.setIamPolicy({
resource: `projects/${projectId}`,
requestBody: resource
});
console.log('IAM roles granted successfully:', response.data);
return response.data;
} catch (error) {
console.error('Error granting IAM roles:', error);
throw error;
}
};
// Usage example
const projectId = 'your-project-id';
const userEmails = ['[email protected]', '[email protected]']; // List of user emails
grantIAMRoles(projectId, userEmails)
.then(() => {
console.log('IAM roles granted successfully for users:', userEmails);
})
.catch(err => {
console.error('Failed to grant IAM roles:', err);
});
I received the error of
Error granting IAM roles: Error: Request contains an invalid argument.
i referred the link here
Can anyone suggest a solution to the error I’m facing!..