Search code examples
slack-apislack

slack api request code


I am trying to make my initial GET call to retrieve the code for my token request. I have searched for hours and I cannot figure out why I am getting a CORS error. I have set the redirect url in the admin panel and included in my query params. I have also tried without it. nothing is working.

It should be a simple as this?

fetch(`https://slack.com/oauth/authorize?scope=identity.basic&client_id=xxxxxxxxx.xxxxxxx`)
  .then(res => console.log(res))

ERROR

Fetch API cannot load https://slack.com/oauth/authorize?scope=identity.basic&client_id=148914445073.148918419105. Redirect from 'https://slack.com/oauth/authorize?scope=identity.basic&client_id=xxxxxxxx.xxxxxxxx' to 'https://slack.com/oauth/authorize?client_id=xxxxxx.xxxxx&scope=identity.basic&team=T4CSWD325' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Solution

  • You need to redirect the user to this page, not load the content of the page asynchronously via fetch :D

    This is the page where the user approves your application, after which they will be redirected to the callback you specified.

    Most apps use the Add to Slack button to add this to their sites.

    More information about the whole OAuth process is here: https://api.slack.com/docs/oauth