プログラミング初心者です。
現在、PythonのフレームワークFastAPIでWebアプリを初めて作成しております。
ユーザー認証機能を作成しているのですが、ユーザー認証時のトークンの発行コード内でTypeErrorが出てきて、解決に困っております。
def create_tokens(user_id:int): """トークンの発行 """ access_payload={ "token_type": "access_token", "exp": datetime.utcnow() + timedelta(minutes=60), "user_id": user_id } refresh_payload={ "token_type": "refresh_token", "exp": datetime.utcnow() + timedelta(days=60), "user_id": user_id } access_token= jwt.encode(access_payload, "SECRET_KEY1234", algorithm="HS256") refresh_token= jwt.encode(refresh_payload, "SECRET_KEY1234", algorithm="HS256") Users.update(refresh_token=refresh_token).where(Users.id == user_id).execute() return {'access_token': access_token, 'refresh_token': refresh_token, 'token_type': 'bearer'}
現在、トークンはサンプルとして上記の様にコードを書きました。
すると以下の様にエラーが発生しました。
FROM users api | WHERE users.name = %s api | LIMIT %s api | 2021-03-12 15:02:39,699 INFO sqlalchemy.engine.base.Engine ('Goku', 1) api | INFO: 172.18.0.1:52272 - "POST /v0/admin/token HTTP/1.1" 500 Internal Server Error api | ERROR: Exception in ASGI application api | Traceback (most recent call last): api | File "/usr/local/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 394, in run_asgi api | result = await app(self.scope, self.receive, self.send) api | File "/usr/local/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__ api | return await self.app(scope, receive, send) api | File "/usr/local/lib/python3.8/site-packages/fastapi/applications.py", line 179, in __call__ api | await super().__call__(scope, receive, send) api | File "/usr/local/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__ api | await self.middleware_stack(scope, receive, send) api | File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__ api | raise exc from None api | File "/usr/local/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__ api | await self.app(scope, receive, _send) api | File "/usr/local/lib/python3.8/site-packages/starlette/middleware/cors.py", line 86, in __call__ api | await self.simple_response(scope, receive, send, request_headers=headers) api | File "/usr/local/lib/python3.8/site-packages/starlette/middleware/cors.py", line 142, in simple_response api | await self.app(scope, receive, send) api | File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__ api | raise exc from None api | File "/usr/local/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__ api | await self.app(scope, receive, sender) api | File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__ api | await route.handle(scope, receive, send) api | File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 227, in handle api | await self.app(scope, receive, send) api | File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 41, in app api | response = await func(request) api | File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 182, in app api | raw_response = await run_endpoint_function( api | File "/usr/local/lib/python3.8/site-packages/fastapi/routing.py", line 135, in run_endpoint_function api | return await run_in_threadpool(dependant.call, **values) api | File "/usr/local/lib/python3.8/site-packages/starlette/concurrency.py", line 34, in run_in_threadpool api | return await loop.run_in_executor(None, func, *args) api | File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run api | result = self.fn(*self.args, **self.kwargs) api | File "./routers/admin.py", line 69, in login api | return create_tokens(user.id) api | File "./routers/admin.py", line 42, in create_tokens api | Users.update(refresh_token=refresh_token).where(Users.id == user_id).execute() api | TypeError: update() got an unexpected keyword argument 'refresh_token'
TypeErrorということですが、これはupdate()の引数の定義の仕方に不備があるのでしょうか?
解決方法が分からない状態です。
何かお力添えを頂けますと嬉しいです。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/03/13 11:18 編集
2021/03/13 12:15
2021/03/13 15:44