Usage with httpx 'auth' #264
-
Can I use httpx-oauth to directly connect authenticated to any url?
I don't want to start a server. Maybe I'm missing a detail. How can I use httpx-oauth to identify myself with such a service?! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @oryon-dominik! Unfortunately, OAuth2 is a bit more complex and involves a bit more steps than that 🙃 I strongly recommend you to read detailed resources about the protocol, but here is a simplified overview: 1. Authorization URLGenerate an authorization URL for the service provider (GitHub, Google...). This URL points to a web page where you'll be able to allow your application to access your personal data (you probably already seen and used this kind of interface). authorization_url = await client.get_authorization_url(
"http://localhost:8000/oauth-callback", scope=["SCOPE1", "SCOPE2", "SCOPE3"],
) The first argument is the redirect URL: it's a URL on your own web server where the user will be redirected when they have clicked on the "Allow"/"Deny" button on the authorization page. You see that OAuth2 is a protocol mainly thought for the web. The scopes are a list of strings determining the things you'll be allowed to do with the API. Every service provider has its own list of scopes to finely determine the level of access to their API. 2. The callbackWhen the user has finished on the authorization page, they are redirected to your redirect URL. If everything went well, you'll have a temporary code in the query parameters of the URL. Something like this:
This code can be used to generate a proper access token to the API. Retrieve it and call the access_token = await client.get_access_token("XXXXXX", "http://localhost:8000/oauth-callback") Notice that you need to put again the exact same redirect URL as a safety check. The token = access_token["access_token"] 3. Use the tokenYou now have an access token to authorize your HTTP calls to the service API. Usually, you need to pass it in an with httpx.Client(headers={"Authorization": f"Bearer {token}"}) as client:
r = client.get(url) |
Beta Was this translation helpful? Give feedback.
Hi @oryon-dominik!
Unfortunately, OAuth2 is a bit more complex and involves a bit more steps than that 🙃 I strongly recommend you to read detailed resources about the protocol, but here is a simplified overview:
1. Authorization URL
Generate an authorization URL for the service provider (GitHub, Google...). This URL points to a web page where you'll be able to allow your application to access your personal data (you probably already seen and used this kind of interface).
The first argument is the redirect URL: it's a URL on your own web server where t…