Last active
May 22, 2021 04:09
-
-
Save nbellocam/65141cb69bcb9d395a4a26e0c034b804 to your computer and use it in GitHub Desktop.
This code shows how to invite users to an Office 365 tenant as guest using Microsoft Graph API from an Azure Function
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
module.exports = function (context, req) { | |
context.log('JavaScript HTTP trigger function processed a request.'); | |
if (req.query.email && req.query.name) { | |
const userEmail = req.query.email; | |
const userDisplayName = req.query.name; | |
getToken().then(token => { | |
/* INVITE A USER TO YOUR TENANT */ | |
var options = { | |
method: 'POST', | |
url: 'https://graph.microsoft.com/v1.0/invitations', | |
headers: { | |
'Authorization': 'Bearer ' + token, | |
'content-type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
"invitedUserDisplayName": userDisplayName, | |
"invitedUserEmailAddress": userEmail, | |
"inviteRedirectUrl": invitationRedirectUrl, | |
"sendInvitationMessage": true, | |
"invitedUserMessageInfo": { | |
"customizedMessageBody": "This is a custom body message" | |
} | |
}) | |
}; | |
request(options, (error, response, body) => { | |
if (!error && response.statusCode == 201) { | |
var result = JSON.parse(body); | |
context.log(result); | |
context.res = { | |
status: 200, | |
body: { | |
id: result.id, | |
inviteRedeemUrl: result.inviteRedeemUrl, | |
status: result.status | |
} | |
}; | |
} else { | |
context.res = { | |
status: response.statusCode, | |
body: result | |
}; | |
} | |
context.done(); | |
}); | |
}); | |
} else { | |
context.res = { | |
status: 400, | |
body: "Please pass a name and email on the query string" | |
}; | |
context.done(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How can I use the getToken() function here?
Where I can import this function?