I am trying to send mail using Graph API from Angular app.
In Azure active directory i have given the API permission for Mail.Send and other mail related things.
below is the code
const resource = {
grant_type : 'client_credentials',
clientID: '****************************',
redirectUri: 'http://localhost:4200/',
validateAuthority : true,
popUp: true,
scopes: ['mail.send'],
resource : 'https://graph.microsoft.com'
};
let murl : any
murl = 'https://graph.microsoft.com/v1.0/users/' + 'testuser@abcd.onmicrosoft.com' + '/sendMail';
this.token = await this.msalService.acquireTokenSilent(resource)
.catch((reason) => {
// console.log(reason);
});
let header = new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + this.token.accessToken});
let options = { headers: header };
let resp = this.http.post(murl, this.sendMailDet, options)
resp.subscribe(
(data)=>{
console.log( data);
}
);
But when i send mail i get below error.
error: {code: "InvalidAuthenticationToken", message: "Access token validation failure. Invalid audience.",…}
code: "InvalidAuthenticationToken"
innerError: {date: "2021-01-06T04:52:20", request-id: "*********",…}
message: "Access token validation failure. Invalid audience."
I am using scopes: ['mail.send'] in resources still i am getting this error. Also i am using accessToken only from this.msalService.acquireTokenSilent(resource).
Token in jwt.ms showing aud as "aud": "https://graph.microsoft.com", and "scp": "Directory.Read.All email Mail.Read Mail.Read.Shared Mail.ReadBasic Mail.ReadWrite Mail.ReadWrite.Shared Mail.Send Mail.Send.Shared MailboxSettings.Read MailboxSettings.ReadWrite User.Export.All User.Invite.All User.ManageIdentities.All User.Read User.Read.All User.ReadBasic.All User.ReadWrite User.ReadWrite.All profile openid",
can anyone please help me to check this issue.
acquireTokenSilent
method is called, library first checks the cache in browser storage to see if a valid token exists and returns it. So in fact you are not getting an application token for Microsoft Graph here. – Allen Wuclient_credentials
as grant_type, which means you want to use application token rather than user token. Is that right? – Allen Wuclient_credentials
flow, there should beroles
claim rather thanscp
claim. Androles
claim is Application permission.scp
claim is Delegated permission. You should be able to find these 2 type of permission when you add it in AAD app. – Allen Wu