CORS issue while getting token with Oauth 2.0 Client credential flow using just React?

Dev 111 Reputation points
2021-04-12T10:19:51.813+00:00

I am able to get the Bearer token from the postman.

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

client_id : <cient-id/app-id>,
scope : "https://graph.microsoft.com/.default",
client_secret:<client-secret>,
grant_type : "client_credentials"

Content-Type : "application/x-www-form-urlencoded"

but same thing when I am doing with React using Fetch() method it's giving me the CORS issue. I am using client_cerdentials flow using Oauth2.0.

86819-image.png

86902-image.png

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,569 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,454 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dev 111 Reputation points
    2021-04-16T05:32:33.093+00:00

    It is not the flow or configuration that is causing the issue. The real issue is front-end or SPA. IF you want to call the API for getting token with client Credential flow,You must follow either of the two approaches that is a mandatory thing I guess.
    1.Daemon Services
    2.Server side Implementation
    I have called the api with Node and with same configuration as mention in the docs its working fine now.
    Thanks

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Danstan Onyango 3,741 Reputation points Microsoft Employee
    2021-04-13T16:57:37.26+00:00

    You are getting cors error because AAD rejects the request when you include Origin header while using client_credentials. To start with You are using the client_credentials flow the wrong way.
    Consider reading through this guide on client credentials flow to help you understand why its preferred for server side / daemon applications.

    To see the error just add Origin header to postman and do the request again.

    Also read here to understand the oauth2 flows that you can use

    For your case, you can use authorization code flow

    1 person found this answer helpful.