6

I am trying to authenticate my app with Slack. It was working perfectly fine for a few days, but now it's throwing me an error

invalid_code

const requestBody = qs.stringify({
    code: code,
    redirect_uri: redirect_uri,
    client_id: client_id,
    client_secret: client_secret
  });

  await axios
    .post(url, requestBody, config).
then(server => console.log(server)).catch(err => console.log(err))

Response from server: { ok: false, error: 'invalid_code' }

The code I get is in this format. code=303414024726.805164905556.526d546072e9b2408e0743f42ca3bb5843553a6c3b930b1de2c1e31847b25448

I think this is JWT token but I am not sure.

Any help would be appreciated.

Sumair Baloch
  • 445
  • 3
  • 15

2 Answers2

0

This is how i made it work

 let slackUrl = `https://slack.com/api/oauth.v2.access`;
let client_id = process.env.SLACK_CLIENT_ID;
let client_secret = process.env.SLACK_CLIENT_SECRET;
let details = {
    code,
    client_id,
    client_secret
}
var formBody = [];
for (var property in details) {
    var encodedKey = encodeURIComponent(property);
    var encodedValue = encodeURIComponent(details[property]);
    formBody.push(encodedKey + "=" + encodedValue);
}
formBody = formBody.join("&");

const _headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
};

let config = {
    method: 'POST',
    url: slackUrl,
    data: formBody,
    headers: _headers
};

let installation = await axios(config);
Haruna Akhmad
  • 111
  • 1
  • 4
0

Ran into the same issue where it worked then kept getting "invalid code" error in the response. Turns out, if there is already an OAuth token for the app/workspace, it will throw this error.

To fix this, go to "OAuth & Permissions" in the App management part of Slack, and revoke all your tokens (at the bottom of page.). On the next request, your request will return a new token.

Li Xia
  • 21
  • 1
  • 2