Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Stewartarmbrecht/0b11b5a3b8ccc5b2f5e3f4b715d5e25a to your computer and use it in GitHub Desktop.
Save Stewartarmbrecht/0b11b5a3b8ccc5b2f5e3f4b715d5e25a to your computer and use it in GitHub Desktop.
Import-email function
export function importEmail(context, req): void {
const parameters = {
assertion: req.headers["x-ms-token-aad-id-token"],
client_id: process.env.WEBSITE_AUTH_CLIENT_ID,
client_secret: process.env.WEBSITE_AUTH_CLIENT_SECRET,
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
requested_token_use: "on_behalf_of",
resource: "https://graph.microsoft.com",
};
post("https://login.microsoftonline.com/microsoft.com/oauth2/token", {form: parameters},
(aadErr, aadResponse, aadMsg) => {
const msgJson = JSON.parse(aadMsg);
get("https://graph.microsoft.com/v1.0/me/", {auth: {bearer: msgJson.access_token}},
(err, response, msg) => {
context.res = {
body: msg,
};
context.done();
});
});
};
Response:
{"assertion":"ey...","client_id":"1d7c723b-af90-40fc-b812-aaf425af8e3d","client_secret":"Ns8...","grant_type":"urn:ietf:params:oauth:grant-type:jwt-bearer","requested_token_use":"on_behalf_of","resource":"https://graph.microsoft.com"}
{"error":"invalid_grant",
"error_description":"AADSTS65001: The user or administrator has not consented to use the application with ID '1d7c723b-af90-40fc-b812-aaf425af8e3d'. Send an interactive authorization request for this user and resource.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment