如何刷新不一致的OAuth2令牌

5tmbdcev  于 2022-09-21  发布在  其他
关注(0)|答案(1)|浏览(248)

到目前为止,这是我的代码

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))

每次我运行它,我都会收到一个400错误

Traceback (most recent call last):
    js[i] = refresh_token(js[i]["refresh_token"])
  File "c:UserscDownloadsdiscord-oauth2-example-masterdiscord-oauth2-example-masterrefresh.py", line 18, in refresh_token
    r.raise_for_status()
  File "C:UserscAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesrequestsmodels.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

代码直接取自不和谐网站,所以我不知道出了什么问题。我最初的代币赠与是有效的,但不是这个。有什么主意吗?谢谢

q0qdq0h2

q0qdq0h21#

我认为您应该在数据对象本身中提供scope密钥。scope是您在创建access_token时初始设置的密钥;

以空格分隔的scope密钥的示例:
identify email guilds

如果您检查discord-oauth2库,它要求我们将“Scope”键传递到数据对象

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

相关问题