How do I refresh discord oauth2 token

Viewed 365

So far this is my code

import requests, json
API_ENDPOINT = 'https://discord.com/api/v8'
CLIENT_ID = '82'
CLIENT_SECRET = 'db'
REDIRECT_URI = 'https://google.com'

def refresh_token(refresh_token):
  data = {
    'client_id': CLIENT_ID,
    'client_secret': CLIENT_SECRET,
    'grant_type': 'refresh_token',
    'refresh_token': refresh_token
  }
  headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
  r = requests.post('%s/oauth2/token' % API_ENDPOINT, data=data, headers=headers)
  r.raise_for_status()
  return r.json()
js = json.loads(open("token.json", "r").read())
for i in js:
     js[i] = refresh_token(js[i]["refresh_token"])
open("token.txt", "w").write(json.dumps(js))

Every time I run it, I get a 400 error

Traceback (most recent call last):
    js[i] = refresh_token(js[i]["refresh_token"])
  File "c:\Users\c\Downloads\discord-oauth2-example-master\discord-oauth2-example-master\refresh.py", line 18, in refresh_token
    r.raise_for_status()
  File "C:\Users\c\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 943, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://discord.com/api/v8/oauth2/token

The code is taken directly from the discord website, so i have no idea what is going wrong. My original token grants are working but not this. Any ideas? Thanks

1 Answers

I think you should provide the scope key in the data object itself. The scope is the key that you initially set when you created the access_token;

Example of scope key that is separated by whitespace:
identify email guilds

If you check the discord-oauth2 library, it requires us to pass the "scope" key into the data object

https://www.npmjs.com/package/discord-oauth2

Related