Skip to content

Instantly share code, notes, and snippets.

@nbellocam
Last active May 22, 2021 04:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbellocam/65141cb69bcb9d395a4a26e0c034b804 to your computer and use it in GitHub Desktop.
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
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();
}
};
@Tushar-Beladiya
Copy link

How can I use the getToken() function here?
Where I can import this function?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment