nwunderly / starlette-discord Goto Github PK
View Code? Open in Web Editor NEW"Login with Discord" support for Starlette and FastAPI
Home Page: https://starlette-discord.rtfd.io
License: MIT License
"Login with Discord" support for Starlette and FastAPI
Home Page: https://starlette-discord.rtfd.io
License: MIT License
oauth.py is a bit of a mess at the moment. It's meant to be an internal class, and it's essentially just a port of a requests
-based implementation that I pulled from a Gist a while ago. It's in desperate need of some love.
Any improvements to the file are welcome, and will be accepted as PRs. This issue will remain open until the file is fully improved.
Some ideas:
This is a bit of a more complicated task. It's likely something I'll revisit in the coming months, and (if necessary) just rewrite from scratch myself. That said, any improvements to library internals are always welcome.
Happy Hacktoberfest!! ๐
Code:
Traceback (most recent call last):
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 375, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\uvicorn\middleware\debug.py", line 96, in __call__
raise exc from None
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\uvicorn\middleware\debug.py", line 93, in __call__
await self.app(scope, receive, inner_send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\fastapi\applications.py", line 208, in __call__
await super().__call__(scope, receive, send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\applications.py", line 112, in __call__
await self.middleware_stack(scope, receive, send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\middleware\errors.py", line 181, in __call__
raise exc
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\middleware\errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\exceptions.py", line 82, in __call__
raise exc
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\routing.py", line 656, in __call__
await route.handle(scope, receive, send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\routing.py", line 259, in handle
await self.app(scope, receive, send)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette\routing.py", line 61, in app
response = await func(request)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\fastapi\routing.py", line 226, in app
raw_response = await run_endpoint_function(
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\fastapi\routing.py", line 159, in run_endpoint_function
return await dependant.call(**values)
File "C:\Users\Dante\Documents\Mai\website\backend\main.py", line 104, in redirect
user = await discord.login(code)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette_discord\client.py", line 292, in login
user = await session.identify()
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette_discord\client.py", line 126, in identify
data_user = await self._discord_request('/users/@me')
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette_discord\client.py", line 103, in _discord_request
self._discord_token = await self.fetch_token(
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\starlette_discord\oauth.py", line 318, in fetch_token
auth = aiohttp.BasicAuth(login=client_id, password=client_secret)
File "C:\Users\Dante\AppData\Local\pypoetry\Cache\virtualenvs\mai-backend-h2M1C5Y5-py3.10\lib\site-packages\aiohttp\helpers.py", line 142, in __new__
if ":" in login:
TypeError: argument of type 'int' is not iterable
When the DiscordOAuthSession
instance fetches the access_token
on callback, the information contained in the token is this:
{'access_token': '...', 'expires_in': 604800, 'refresh_token': '...', 'scope': ['identify', 'guilds'], 'token_type': 'Bearer', 'expires_at': 1621962039.1969795}.
(tokens removed for obvious reasons).
This information is lost in the examples supplied but can be preserved in this way:
client = DiscordOAuthClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)
....
@app.get('/callback')
async def callback(code: str):
async with client.session(code) as session:
token = session._discord_token # notice protected attribute access
The expires_in
field gives the amount of seconds that the token should last before it expires, in this case its 604800 which amounts to one week.
As follows, its unreasonable that the same token information cannot be used in following requests.
The DiscordOAuthSession instance must be able to be constructed from a valid token instead of only with the code
# following the client's construction
client = DiscordOAuthClient(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI)
# sessions should be able to be constructed from the exchanged code on login redirect
session = client.session(code='dhjaksdhjadskhdaskjda')
# or with a stored, valid token
session = client.session(token='dsjkdsaljdaskldas')
Aditionally, it would also be useful if there was a token_updater
method that would make use of the supplied refresh_token
.
This issue causes that installing the package from pip ( pip install -U starlette-discord
) wont install the requirements as well, meaning you need to manually install discord.py, oauthlib and starlette.
Proposed fix: add the dependencies to the setup file.
Hi, I'm currently trying to get all guild data that a user is a part of using
async with client.session(code) as session: user = await session.identify() guilds = await session.guilds() connections = await session.connections() print(guilds)
while following the examples to get an understanding of how to build my API, but I'm only getting the ID of the guild and the name are this a known issue or am I missing some steps
Some documentation in oauth.py and client.py is documented using the wrong docstring style (generic RST format, we want NumPy style).
Need documentation for these methods reformatted. Bonus points for any additional improvements to documentation, like rewording or fixing formatting mistakes elsewhere.
We want to make our data classes in models.py
compatible with Pydantic in order to facilitate better interoperability with API and database use cases.
What would need to be done:
Happy Hacktoberfest! ๐
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.