Skip to content

Instantly share code, notes, and snippets.

@angelxmoreno
Created December 13, 2023 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angelxmoreno/074ea4e6baa83ec9df8e1bfbb3f298e0 to your computer and use it in GitHub Desktop.
Save angelxmoreno/074ea4e6baa83ec9df8e1bfbb3f298e0 to your computer and use it in GitHub Desktop.
exchange_token
function _encode(obj: object) {
let string = "";
for (const [key, value] of Object.entries(obj)) {
if (!value) continue;
string += `&${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
}
return string.substring(1);
}
const config = {
'client_id': this.config.clientId,
'client_secret': this.config.clientSecret,
'grant_type': 'authorization_code',
'code': String(code),
'redirect_uri': this.config.callbackUrl,
'scope': this.config.scopes.join(' ')
}
const params = _encode(config)
const response = await fetch(`https://discordapp.com/api/oauth2/token`,
{
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: params
});
const oauthData = await response.json()
@angelxmoreno
Copy link
Author

oauthData:

{
  error: 'invalid_grant',
  error_description: 'Invalid "code" in request.'
}

params:

  params: 'client_id=--redacted--&client_secret=--redacted--&grant_type=authorization_code&code=DOHX20uX176gVEwj0dhK3WPb1kW3vu&redirect_uri=http%3A%2F%2Flocalhost%3A5173&scope=identify'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment