我非常熟悉自库,并试图理解它的概念。
我试图理解,如何使用authlib保存和重用获取的令牌。
我创建了小型FastAPI项目:
from fastapi import FastAPI
from starlette.config import Config
from starlette.middleware.sessions import SessionMiddleware
from starlette.requests import Request
from authlib.integrations.starlette_client import OAuth
app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="some-random-secret")
config = Config(".env")
oauth = OAuth(config)
oauth.register(
name="some_service",
client_id="client_id",
client_secret="client_secret",
authorize_url="https://some-service.com/auth",
access_token_url="https://some-service.com/token",
client_kwargs={
"token_endpoint_auth_method": "client_secret_post",
},
)
@app.get("/login")
async def login(request: Request):
redirect_uri = "https://myservice.com/auth"
return await oauth.some_service.authorize_redirect(request, redirect_uri)
@app.get("/auth")
async def auth(request: Request):
token = await oauth.some_service.authorize_access_token(request)
# I suppose that I should save somehow token here
return token
@app.get("/account")
async def get_account(request: Request):
account_url = "https://some-service.com/account"
resp = await oauth.some_service.get(account_url)
return resp.json()我想知道账户信息。因此,进一步的步骤将是:
GET /login我将允许使用我的帐户,并将重定向回我的服务。
GET /auth?oauth_params1=foo&oauth_params2=bar将从令牌提供程序获取令牌。我知道我错误地以为这个记号一定会被保存在某个地方。
GET /account在这里,我期望通过OAuth客户端,我可以发送以前获取的令牌。但是,我会发现下一个错误:
authlib.integrations.base_client.errors.MissingTokenError: missing_token:我也知道我应该提供这样的标记:
oauth.some_service.get(account_url, token=previously_fetched_token)但是,我不想每次从some-service询问令牌,我想重用令牌。怎么做?
我是不是错了,这个问题是authlib范围的一部分?我应该用缓存或数据库机制找到解决方案吗?
我也是FastAPI的初学者.
发布于 2021-12-06 00:38:49
token是一个具有多个值的对象-
{
"oauth_token": "TOKEN ID",
"oauth_token_secret": "SECRET TOKEN",
"user_id": "USER ID",
"screen_name": "USER SCREEN NAME"
}你有几个选择-
https://stackoverflow.com/questions/69790405
复制相似问题