実現したいこと
discordにてoauth認証でアクセストークンを取得し、そのトークンを使ってとあるサーバーに参加させたい
前提
サーバーに参加させるときに
{"message": "Invalid OAuth2 access token", "code": 50025}
が返されてしまい、参加処理が行われない
xserverで取得したドメインで、xserver上でflask.py
を動かしています
発生している問題・エラーメッセージ
{"message": "Invalid OAuth2 access token", "code": 50025}
flask.py(アクセストークンを取得するプログラム)
python
1from flask import Flask, request 2import requests 3import json 4app = Flask(__name__) 5 6client_id = "xxxxxxxxxxxxxx" 7client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 8redirect_uri = "https://xxxxx.com/callback" 9scopes = ["guilds.join","identify","guilds"] # スコープ 10 11 12@app.route("/callback") 13def callback(): 14 15 16# アクセストークンの取得 17 token_endpoint = "https://discord.com/api/oauth2/token" 18 data = { 19 "grant_type": "authorization_code", 20 "client_id": client_id, 21 "client_secret": client_secret, 22 "redirect_uri": redirect_uri, 23 "scope": scopes, 24 "code": request.args.get("code") 25 } 26 headers = { 27 "Content-Type": "application/x-www-form-urlencoded" 28 } 29 response = requests.post(token_endpoint, data=data, headers=headers) 30 if response.status_code == 200: 31 # アクセストークンの取得に成功した場合 32 access_token = response.json()["access_token"] 33 # 取得したアクセストークンを使用してDiscordのAPIを呼び出し、サーバーに参加させるリクエストを送信う 34 print("Access token:", access_token) 35 else: 36 return f"{response.text}" 37 return f"Code: {access_token}, Token: {response.text}" 38if __name__ == "__main__": 39 app.run()
main.py(参加処理を実行するプログラム)
python
1 2import discord 3 4import requests 5from discord.ext import commands 6import discord 7import requests 8from discord.ext import commands 9from discord.ext.commands import Bot 10intents = discord.Intents.all() 11from discord.ext import commands 12from discord.ui import * 13 14 15bot = commands.Bot(command_prefix='!',intents=intents) 16 17 18 19import discord 20import requests 21 22@bot.event 23async def on_ready(): 24 print(f"Logged in as {bot.user.name}") 25 26 27 # Botが参加しているサーバー一覧を表示 28 print("Bot is ready and connected to the following servers:") 29 for guild in bot.guilds: 30 print(f"- {guild.name} (id: {guild.id})") 31 32 33 34 35 36token = "botのtoken" 37@bot.slash_command(name="memberrestore", description="認証済みユーザーを参加させます") 38async def unban_allw(ctx): 39 roll = "1097864843232559215" 40 guild_id=ctx.guild_id 41 user=ctx.user.id 42 await ctx.send("開始します") 43 44 a=0 45 for i in range(1): 46 47 user="12345656767878" 48 path="backup/user/{}.txt".format(user) 49 accesstoken="ここにflask.pyで取得したアクセストークン" 50 head = {"Authorization": 'Bot ' + token, 'Content-Type': 'application/json'} 51 json = {"access_token":accesstoken} 52 53 dd=requests.put(f'https://discord.com/api/v10/guilds/' + str(guild_id) + '/members/' + str(user),json=json, headers=head) 54 print (dd.text) 55 ddd = requests.put("https://discord.com/api/v10/guilds/{}/members/{}/roles/{}".format(str(guild_id),str(user),str(roll)), headers=head) 56 print (ddd.text) 57 if str(dd)=="<Response [201]>": 58 a=a+1 59 await ctx.send("{}人のBackUpが完了しました".format(a)) 60 print (dd.text) 61 62 print (f"失敗{dd.text}") 63 64bot.run("token")
discord developerに登録したリダイレクト先url
https://xxxxx.com/callback
試したこと
スコープの設定を何通りか変えました
あとはかなり調べましたがわかりませんでした・・・
アクセストークンを取得する部分に間違いがあるのかもしれないのでどなたかご教授をおねがいします><
補足情報(FW/ツールのバージョンなど)
python3.10.0
xserver

あなたの回答
tips
プレビュー