4

I'm trying to get a token using the Outlook REST API after I got the code from this url https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize

I used a POST request with axios but the issue is I got this error:

Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

My Axios post is this:

return Axios({
    method: 'post',
    url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Access-Control-Allow-Origin':'*', Content-Type, origin, authorization, accept, client-security-token"
    },
    data: {
        'client_id': 'xxxxxx',
        'scope': 'calendars.readwrite',
        'code': encodeURIComponent('000000000'),
        'redirect_uri': encodeURIComponent('http://localhost:3000/storeprofile/xxxxxxxxx/outlook-scheduler'),
        'grant_type': 'authorization_code',
        'client_secret': encodeURIComponent('xxxxxxxxxx')
    };,
    responseType: 'json'
    }).then((response) => {
        console.log('token response: ', response);
    });

I get a 200 status but can't see the token I am supposed to get in the response. The response should be a json but still nothing.

Can someone please guide me? Thank you in advance!

Jeremy Thake MSFT
  • 2,058
  • 2
  • 13
  • 11
paolacl
  • 191
  • 2
  • 6
  • 3
    This is a duplicate of: https://stackoverflow.com/q/47185296/1658906. **Do not use authorization code flow from front-end JavaScript!** Your client secret is now public. – juunas Dec 14 '17 at 20:40
  • Hi @paolacl were you able to resolve this out ? – NitinSingh May 29 '18 at 07:00
  • you just need to change the app type from WEB to SPA. See here: https://stackoverflow.com/questions/59844516/how-to-enable-cors-in-an-azure-app-registration-when-used-in-an-oauth-authorizat – ncesar Nov 05 '20 at 22:34

1 Answers1

0

When you register your app, you must setup the Redirect URI/URL, usually this url will be added to the CORS header.

doc : https://developer.microsoft.com/en-us/graph/docs/concepts/auth_overview#how-do-i-get-my-app-talking-to-azure-ad-and-microsoft-graph

Gabriel Bleu
  • 9,703
  • 2
  • 30
  • 43
  • 1
    thanks for your comment, I added the redirect uri/url when I created the app on Outlook but still nothing yet. – paolacl Nov 03 '17 at 18:21