ardaozmen
ardaozmen

Reputation: 3

Microsoft OAuth 2.0 Authentication Failure - token_url Bad Request Error

I want to ensure that my OAuth 2.0 authentication credentials. So, I have client_id, client_secret, tenant_id, scope, token_url and auth_url. I am using third party app in order to send a email but, I get error in OAuth 2.0 part as Bad Request Error for token_url.

In this case, when grant_type='client_credentials', how can I fix token_url domains or subdomains?

-> token_url like that https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token

If you are dominate the this subject, Could you please mention about OAuth 2.0 process?

I tried to changed grant_type parameter as client_credentials. Default was 'refresh_token'.

In this case, it returned like that error:

(530, b'5.7.57 Client not authenticated to send mail. Error: 535 5.7.3 Authentication unsuccessful [ZR2P278CA0041.CHEP278.PROD.OUTLOOK.COM 2023-02-14T08:10:26.343Z 08DB0DEF6EA5D39B]', '[email protected]')
2023-02-14 11:10:26,378 INFO [decorators] [send_email] StatusMessage: An error occurred while sending the email: (530, b'5.7.57 Client not authenticated to send mail. Error: 535 5.7.3 Authentication unsuccessful [ZR2P278CA0041.CHEP278.PROD.OUTLOOK.COM 2023-02-14T08:10:26.343Z 08DB0DEF6EA5D39B]', '[email protected]')
2023-02-14 11:10:26,379 INFO [decorators] [send_email] StatusMessage: Done with sending email...

So, what is the main problem in this case?

Thank you for your attention,

Upvotes: 0

Views: 814

Answers (1)

Imran
Imran

Reputation: 5540

I tried to reproduce the same in my environment and got the results as below: 

I created an Azure AD Application and granted admin consent to Mail.Send API permission:

 enter image description here 

Based on your requirement you can make use of Client Credential Flow or Authorization Code Flow.

  • If you want user interaction then make use of Authorization Code Flow.
  • If you want to send mail as Application then make use of Client Credential Flow. 

I generated the access token via Client Credential Flow by using parameters like below:

https://login.microsoftonline.com/TenantID/oauth2/v2.0/token

client_id:ClientID
client_secret:ClientSecret
scope:https://graph.microsoft.com/.default
grant_type:client_credentials

enter image description here 

 To send the mail, I used below query:

https://graph.microsoft.com/v1.0/users/FromAddress/sendMail 

{
"message": {
"subject": "Test mail",
"body": {
"contentType": "Text",
"content": "Test"
},
"toRecipients": [
{
"emailAddress": {
"address": "****"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"address": "****"
}
}
]
},
"saveToSentItems": "false"
}

 enter image description here

 Reference: 

user: sendMail - Microsoft Graph v1.0 | Microsoft Learn

Upvotes: 0

Related Questions