Last active
June 7, 2024 19:12
-
-
Save ivansnag/6d62f56e66df1cf7393b040820b902d2 to your computer and use it in GitHub Desktop.
A CSV with the column name "email" is required. The name of that file should be set on line 73. Instructions including where to locate Constants and what permissions are required linked: https://docs.google.com/document/d/1maUzdJ6t5gpToZYmDQ1dYNLs81h1aMmd4PuRmuZ5CSs/edit?usp=sharing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const axios = require('axios'); | |
const fs = require('fs'); | |
const csv = require('csv-parser'); | |
// Constants - replace these with your actual values | |
const CLIENT_ID = ''; | |
const CLIENT_SECRET = ''; | |
const TENANT_ID = ''; | |
const TEAM_ID = ''; | |
const getAccessToken = async () => { | |
const url = `https://login.microsoftonline.com/${TENANT_ID}/oauth2/v2.0/token`; | |
const params = new URLSearchParams(); | |
params.append('grant_type', 'client_credentials'); | |
params.append('client_id', CLIENT_ID); | |
params.append('client_secret', CLIENT_SECRET); | |
params.append('scope', 'https://graph.microsoft.com/.default'); | |
try { | |
const response = await axios.post(url, params); | |
return response.data.access_token; | |
} catch (error) { | |
console.error('Error getting access token:', error); | |
} | |
}; | |
const getUserId = async (accessToken, email) => { | |
if (!email) { | |
console.error('Email is undefined. Check if it is being correctly passed to getUserId.'); | |
return null; | |
} | |
const url = `https://graph.microsoft.com/v1.0/users?$filter=userPrincipalName eq '${email}'`; | |
console.log('Requesting user ID for email:', email); // Log the email being requested | |
console.log('Using URL:', url); // Log the full request URL to check if it's constructed correctly | |
try { | |
const response = await axios.get(url, { headers: { Authorization: `Bearer ${accessToken}` } }); | |
console.log('API Response:', response.data); // Log the response from API | |
const users = response.data.value; | |
if (users.length > 0) { | |
console.log('User ID found:', users[0].id); // Log the found user ID | |
return users[0].id; | |
} else { | |
console.error('No user found for this email:', email); | |
return null; | |
} | |
} catch (error) { | |
console.error('Error fetching user ID for email:', email, error.response ? error.response.data : error.message); | |
return null; | |
} | |
}; | |
const addUserToTeam = async (accessToken, teamId, userId) => { | |
const url = `https://graph.microsoft.com/v1.0/teams/${teamId}/members`; | |
const data = { | |
'@odata.type': '#microsoft.graph.aadUserConversationMember', | |
'roles': ['owner'], | |
'[email protected]': `https://graph.microsoft.com/v1.0/users('${userId}')` | |
}; | |
try { | |
const response = await axios.post(url, data, { headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json' } }); | |
return response.data; | |
} catch (error) { | |
console.error('Error adding user to team:', error); | |
} | |
}; | |
const main = async () => { | |
const results = []; | |
fs.createReadStream('users_to_add_to_team.csv') | |
.pipe(csv()) | |
.on('data', (data) => results.push(data)) | |
.on('end', async () => { | |
const accessToken = await getAccessToken(); | |
for (const row of results) { | |
const userId = await getUserId(accessToken, row.email); | |
if (userId) { | |
const result = await addUserToTeam(accessToken, TEAM_ID, userId); | |
console.log(`Added user with email ${row.email}:`, result); | |
} else { | |
console.log(`User not found for email ${row.email}`); | |
} | |
} | |
}); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment