You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I had to write this here, after spending a couple of evenings trying to figure out how to use the eminent openapi-python-client with the FastAPI security example.
I'm a python learner and just starting out but very impressed how quickly I could whip up a REST service with competent DB-integration and just the right functionality I need for a data logging system. This is done with the eminent FastAPI project and all the well-written examples for different types of REST services, However, if you consider to put your project on a cloud service or expose your server on the open internet you have to of course "USE SOME BASIC SECURITY" for this service.
Hence, the well written and pedagogic four-step tutorial on https://fastapi.tiangolo.com/tutorial/security/ is the natural starting point. I did all those code examples and could verify the "OAuth2 with Password and Bearer with JWT tokens" security chain, with the web page and the Postman client program. Nice, this is going well was my gut feeling. I only need to translate this into a few lines of python code with the support of the openapi-python-client generator and all those files that are produced under the directory fast-api-client in my project.
Well, I felt stupid as h*ll when I couldn't figure out what went were, and where to put what. OK I'm a newbie on Python but have been "programming this and that" for decades. Google was my friend for a long time but finally I figured out the minimum lines of code I had to use. This is for the client project for the final example https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/ where you actually have a secure stack. Using this together with HTTPS could probably be considered secure. This is the client code:
from fast_api_client import AuthenticatedClient, Client
from fast_api_client.api.default import read_users_me_users_me_get, login_token_post
from fast_api_client.models import BodyLoginTokenPost
login = BodyLoginTokenPost(username="johndoe",password="secret")
loginClient = Client(base_url="http://127.0.0.1:8000")
loginResult = login_token_post.sync_detailed(client=loginClient,body=login)
print(loginResult)
if loginResult.status_code == 200:
client = AuthenticatedClient(base_url="http://127.0.0.1:8000",token=loginResult.parsed["access_token"], verify_ssl=False,follow_redirects=True)
response = read_users_me_users_me_get.sync_detailed(client=client)
print(response)
else:
print("Could not login")``
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I had to write this here, after spending a couple of evenings trying to figure out how to use the eminent openapi-python-client with the FastAPI security example.
I'm a python learner and just starting out but very impressed how quickly I could whip up a REST service with competent DB-integration and just the right functionality I need for a data logging system. This is done with the eminent FastAPI project and all the well-written examples for different types of REST services, However, if you consider to put your project on a cloud service or expose your server on the open internet you have to of course "USE SOME BASIC SECURITY" for this service.
Hence, the well written and pedagogic four-step tutorial on https://fastapi.tiangolo.com/tutorial/security/ is the natural starting point. I did all those code examples and could verify the "OAuth2 with Password and Bearer with JWT tokens" security chain, with the web page and the Postman client program. Nice, this is going well was my gut feeling. I only need to translate this into a few lines of python code with the support of the openapi-python-client generator and all those files that are produced under the directory fast-api-client in my project.
Well, I felt stupid as h*ll when I couldn't figure out what went were, and where to put what. OK I'm a newbie on Python but have been "programming this and that" for decades. Google was my friend for a long time but finally I figured out the minimum lines of code I had to use. This is for the client project for the final example https://fastapi.tiangolo.com/tutorial/security/oauth2-jwt/ where you actually have a secure stack. Using this together with HTTPS could probably be considered secure. This is the client code:
Beta Was this translation helpful? Give feedback.
All reactions