認証時のトークン発行コードのエラー
解決したいこと
プログラミング初心者です。
現在、PythonのフレームワークFastAPIでWebアプリを初めて作成しております。
ユーザー認証機能を作成しているのですが、ユーザー認証時のトークンの発行コード内でTypeErrorが出てきて、解決に困っております。
現在、トークンはサンプルとして下記の様にコードを書きました。
TypeErrorということですが、これはupdate()の引数の定義の仕方に不備があるのでしょうか?
何かお力添えを頂けますと嬉しいです。
発生している問題・エラー
出ているエラーメッセージを入力
例)
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'
または、問題・エラーが起きている画像をここにドラッグアンドドロップ
該当するソースコード
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'}v
Users定義は下記になります。
from datetime import datetime
from sqlalchemy import Column, String, Integer, DateTime
from sqlalchemy.orm import relationship
from passlib.context import CryptContext
from settings.db import Base
from .groups import users_groups
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
class Users(Base):
"""ユーザーアカウント."""
__tablename__ = "users"
name = Column(String(50), nullable=False)
email = Column(String(50), nullable=False, unique=True)
password = Column(String(255), nullable=False)
age = Column(Integer, nullable=False)
gender = Column(String(3), nullable=False)
height = Column(Integer, nullable=False)
weight = Column(Integer, nullable=False)
kind_of_sport = Column(String(6), nullable=False)
type_of_team = Column(String(20), nullable=False)
years_of_experience = Column(Integer, nullable=False)
group_id = Column(Integer, nullable=True)
last_login = Column(DateTime, nullable=False, default=datetime.now)
created_at = Column(DateTime, nullable=False, default=datetime.now)
updated_at = Column(DateTime, nullable=True, default=None)
refresh_token = Column(String(255), nullable=True)
objectives = relationship(
"Objectives",
back_populates="user",
uselist=True,
order_by="Objectives.id",
cascade="all, delete",
passive_deletes=True
)
groups = relationship(
"Groups",
back_populates="users",
uselist=True,
order_by="Groups.id",
secondary=users_groups
)
def __init__(
self,
name: str,
email: str,
password: str,
age: int,
gender: str,
height: int,
weight: int,
kind_of_sport: str,
type_of_team: str,
years_of_experience: int,
group_id:int,
refresh_token:str
):
self.name = name
self.email = email
self.password = pwd_context.hash(password)
self.age = age
self.gender = gender
self.height = height
self.weight = weight
self.kind_of_sport = kind_of_sport
self.type_of_team = type_of_team
self.years_of_experience = years_of_experience
self.group_id = group_id
self.refresh_token = refresh_token
自分で試したこと
ここに問題・エラーに対して試したことを記載してください。
0