Search code examples
nestjsslackslack-api

Stuck on the third step of the V2 OAuth authentication flow


I have created my first bot, and now I'd like to submit to the Slack App directory. As the tutorial says, I should create an endpoint which for Slack to reach when the user approves my app. Slack should send me a temporary code, which in turn I should send back to their OAuth API. This is the relevant code (it's a NestJS app btw):

@Get()
auth(@Query('code') code: string) {
  const url = 'https://slack.com/api/oauth.v2.access';
  const message = {
    client_id: 'my.client.id',
    client_secret: 'my.client.secret',
    code: 'my.code'
  }
  return this.httpService.post(url, message);
}

However, I'm getting the following error:

{
  "ok": false,
  "error": "invalid_arguments",
  "response_metadata": {
    "messages": [
      "[ERROR] missing required field: code"
    ]
  }
}

What am I doing wrong?


Solution

  • Andrea,

    When I look at the slack api definition for that method:

    https://api.slack.com/methods/oauth.v2.access

    I noticed it says

    Present arguments as parameters in application/x-www-form-urlencoded querystring or POST body. This method does not currently accept application/json.
    

    Are you passing that header or url encoding the parameters?

    'Content-Type': 'application/x-www-form-urlencoded',
    

    The reason I ask is that if I visit something like this in a browser

    https://slack.com/api/oauth.v2.access?client_id=00000&client_secret=11111&code=33333
    

    I actually get

    {"ok":false,"error":"invalid_code"}
    

    which is correct because I provided a code in the url parameters list - albeit an invalid code.