question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] HTTP 400 on POST /api/v7/oauth2/token

See original GitHub issue

OAuth URL directs to /oauth/get_token no problem with code, gives 400 on redirecting to /oauth/callback

environment variables:

OAUTH_CLIENT_ID=1234
OAUTH_CLIENT_SECRET=5678
OAUTH_REDIRECT_HOST=http://localhost:3001

my code:

process.env.NODE_ENV = process.env.NODE_ENV || "development";

require("dotenv-cra").config({
  path: __dirname + "/.env"
});

const express = require("express");
const cors = require("cors");
const DiscordOauth2 = require("discord-oauth2");
const oauth = new DiscordOauth2();

const app = express();

app.use(cors());

app.use("/oauth/get_token", async (req, res) => {
  const token = await oauth.tokenRequest({
    clientId: process.env.OAUTH_CLIENT_ID,
    clientSecret: process.env.OAUTH_CLIENT_SECRET,
 
    code: req.query.code,
    scope: "identify guilds",
    grantType: "authorization_code",

    redirectUri: process.env.OAUTH_REDIRECT_HOST + "/callback",
  });

  console.log(token);
});

app.use("/oauth/callback", async (req, res) => {
  console.log(req.headers);
  console.log(req.query);
  console.log(req.body);
  res.send("thanks");
});

const PORT = process.env.PORT || "3001";
app.listen(PORT, e => {
  if (e) throw e;
  console.log("Listening at port " + PORT);
});

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9

github_iconTop GitHub Comments

3reactions
gear4scommented, Jun 3, 2020

I’m an idiot, never mind

0reactions
gear4scommented, Mar 25, 2021

you should open a new ticket instead of commenting here. this ticket is closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

400 Bad Request on POST /api/v7/oauth2/token #16 - GitHub
It does appear that it is due to Discord fixing a bug based on the discussions in the Discord API guild. 4
Read more >
400 Error when requesting a token from Discord API
After further research, I discovered the following video which helped me out. Here is the working code:
Read more >
Why do I get Error code 400 "bad Request" when posting ...
Why do I get Error code 400 "bad Request" when posting HTTP request to token endpoint to exchange for an Oauth2 access token...
Read more >
Bad request 400 when trying to retrieve the Access Token
Hi guys, I'm trying to use the authentication process, but when I make the POST to the app to retrieve the Access Token...
Read more >
Fixes, Limitations, and Known Issues - ForgeRock Backstage
OPENAM-16703: OAuth2 Access token obtained from refresh token is certificate-bound regardless of "Certificate-Bound Access Tokens" configuration (when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found