trying to get access token from graph api to be able to send mail from app.

Pat Sinclair 20 Reputation points
2023-09-15T20:19:13.96+00:00

when trying to get the access token so that I can call graph mail.send, I am getting a 400 bad request.

below is the rest api I am calling:

https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token?client_id={app id}&scope={have tried pointing to graph and the api:appid}&client_secret={tried value and secret id}&grant_type=client_credentials

no matter what I try I get a 400 error...

Have been going round and round with this trying to get the app to work, need to be able to send custom emails.

Thanks up front for any assistance.

Regards,

Pat

Microsoft Graph Applications API
Microsoft Graph Applications API
A Microsoft API that enables you to manage these resources and actions related to applications in Azure Active Directory.
650 questions
Microsoft Graph Mail API
Microsoft Graph Mail API
A Microsoft API that supports accessing data in users' primary mailboxes and in shared mailboxes.
908 questions
0 comments No comments
{count} votes

Accepted answer
  1. CarlZhao-MSFT 28,531 Reputation points
    2023-09-18T03:13:36.39+00:00

    Hi @Pat Sinclair

    Don't try to put the app ID in the scope, this token should only be used to call the graph API and not other web APIs.

    Also, this is a POST request instead of a GET request, don't try to request the URL in the browser, you can send the token request in an API client tool such as Postman.

    POST https://login.microsoftonline.com/{tenant id}/oauth2/v2.0/token           
    Content-Type: application/x-www-form-urlencoded
    
    client_id={client id}
    &scope=https://graph.microsoft.com/.default
    &client_secret={client secret}
    &grant_type=client_credentials
    

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


1 additional answer

Sort by: Most helpful
  1. Vahid Ghafarpour 6,230 Reputation points
    2023-09-15T20:51:12.6166667+00:00

    You can use tools like Postman or cURL to test your token request outside of your application, which can help isolate the issue.