-
I was able to login and redirect and get the user response object and code. How can i get more info from discord? imports...
app = FastAPI()
scopes = ('identify', 'guilds')
discord_client = DiscordOAuthClient(client_id, client_secret, redirect_uri, scopes=scopes)
@app.get('/login')
async def start_login():
return discord_client.redirect()
@app.get('/callback')
async def finish_login(request: Request, code: str):
user = await discord_client.login(code)
user['code'] = code
user_id = user['id']
request.session[user_id] = user
return RedirectResponse(f'/users/{user_id}/guilds')
@app.get('/users/{user_id}/guilds')
async def user_guilds(request: Request, user_id: str):
user = request.session.get(user_id)
print(user)
async with discord_client.session(user['code']) as session:
return await session.guilds()
app.add_middleware(SessionMiddleware, secret_key=secrets.token_urlsafe(64)) The code above works all the way until user_guilds as i'm getting an "invalid code" error. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
async def login(self, code):
"""Shorthand for session setup + identify()"""
async with self.session(code) as session:
user = await session.identify()
return user Since a code can only be used once, the new session's attempt to get the user's guilds with the same code will throw that error. In order to make multiple API requests, you're going to want to use @app.get('/callback')
async def finish_login(request: Request, code: str):
async with discord_client.session(code) as session:
user = await session.identify()
guilds = await session.guilds()
request.session['user'] = user
request.session['guilds'] = guilds
user_id = user['id']
return RedirectResponse(f'/users/{user_id}/guilds')
@app.get('/users/{user_id}/guilds')
async def user_guilds(request: Request, user_id: str):
user = request.session.get('user')
guilds = request.session.get('guilds')
print(user)
return guilds Note that, in addition to this only using the code once, I'm not storing the user data based on id. The whole point of session middleware, what makes it useful, is that the session (as an attribute of Request) is guaranteed to be unique per user. So you can put a user's info under keys like "guild" and "user" and a user will only see their own data. This also means you don't need the user_id path parameter. I left it in the code, but you could just do /users/guilds and the session middleware will handle identifying the user who it applies to. |
Beta Was this translation helpful? Give feedback.
client.login()
is just a "one-shot" shortcut for thesession.identify()
method with internal session handling, after which it kills the session:Since a code can only be used once, the new session's attempt to get the user's guilds with the same code will throw that error. In order to make multiple API requests, you're going to want to use
identify()
within yourasync with
context manager, the same way you're usingsession.guilds()
now. Perhaps something like this: