fastAPI 的 FastAPI-Login 庫,登入後請求介面老是提示 “Not authenticated”

五月朝露發表於2020-04-09
from fastapi import Depends
from fastapi.security import OAuth2PasswordRequestForm
from fastapi_login.exceptions import InvalidCredentialsException

import uvicorn

from fastapi import FastAPI

SECRET = "your-secret-key"

app = FastAPI()

from fastapi_login import LoginManager
manager = LoginManager(SECRET, tokenUrl='/auth/token')

fake_db = {'johndoe@e.mail': {'password': 'hunter2'}}

@manager.user_loader
def load_user(email: str): # could also be an asynchronous function
user = fake_db.get(email)
return user

@app.post('/auth/token')
def login(data: OAuth2PasswordRequestForm = Depends()):
print(data)
email = data.username
password = data.password

user = load_user(email) # we are using the same function to retrieve the user
if not user:
raise InvalidCredentialsException # you can also use your own HTTPException
elif password != user['password']:
raise InvalidCredentialsException

access_token = manager.create_access_token(
data=dict(sub=email)
)
return {'access_token': access_token, 'token_type': 'bearer'}

@app.get("/hello") # 就是這裡
def read_root(user=Depends(manager)):
return {"Hello": "World","esvdgsjv":"wertghvqw3etghvbc"}

if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8000)

執行後,postman登入介面 127.0.0.1/auth/token,登入後獲得token。可是127.0.0.1/hello這個介面怎麼請求的?token放那裡?怎麼放的?

相關文章