简体   繁体   English

FastAPI AttributeError: 'Depends' object 没有属性 'query'

[英]FastAPI AttributeError: 'Depends' object has no attribute 'query'

I'm trying to access my database with fast API. I'm having some problems getting access to it if done it outside of an endpoint.我正在尝试使用快速 API 访问我的数据库。如果在端点之外访问它,我会遇到一些问题。

I'm trying to access the DB and retrieve the allowed emails.我正在尝试访问数据库并检索允许的电子邮件。 I can do that in the get/post (/refresh) endpoints, but not in the route (/token), it throws the error: ''Depends' object has no attribute 'query''我可以在 get/post (/refresh) 端点执行此操作,但不能在路由 (/token) 中执行此操作,它会抛出错误:''Depends' object has no attribute 'query''

From what I understand the depends can only be solved in an API route, so I tried to create a new post endpoint and call it from the route.据我了解,依赖只能在 API 路由中解决,因此我尝试创建一个新的 post 端点并从路由中调用它。 The result is that the request gets stuck...结果就是请求卡住了……

@router.route('/login')
async def login(request: Request):
    #REQUEST GET STUCK IN PENDING
    await requests.post(url="http://localhost:8000/auth" + '/set-users')
    
    redirect_uri = FRONTEND_URL  # This creates the url for our /auth endpoint
    return await oauth.google.authorize_redirect(request, redirect_uri)

@router.post('/set-users')
def set_useres(database: Session = Depends(db_handler.get_db)):
    emails = brand_operation_handler.get_all_users(db=database)
    print(emails)
    allowed_email_in_user_table_setter(emails)
    return ''

@router.route('/token')
async def auth(request: Request, database: Session = Depends(db_handler.get_db)): #,  database: Session = Depends(db_handler.get_db)
    try:
        access_token = await oauth.google.authorize_access_token(request)
    except OAuthError:
        raise CREDENTIALS_EXCEPTION
    user_data = await oauth.google.parse_id_token(request, access_token)

    
    #emails = brand_operation_handler.get_all_users(db=database)
    # allowed_email_in_user_table_setter(emails)



    if (valid_email_from_db(user_data['email'])): #or valid_email_from_user_table(user_data['email'])
        return JSONResponse({
            'result': True,
            'access_token': create_token(user_data['email']),
            'refresh_token': create_refresh_token(user_data['email']),
            'user_data': user_data
        })
    raise CREDENTIALS_EXCEPTION


@router.post('/refresh')
async def refresh(request: Request, database: Session = Depends(db_handler.get_db)):
    
    #THIS WORKS
    emails = brand_operation_handler.get_all_users(db=database)
    print(emails)
    allowed_email_in_user_table_setter(emails)

    try:
        # Only accept post requests
        if request.method == 'POST':
            form = await request.json()
            if form.get('grant_type') == 'refresh_token':
                token = form.get('refresh_token')
                payload = decode_token(token)
                # Check if token is not expired
                if datetime.utcfromtimestamp(payload.get('exp')) > datetime.utcnow():
                    email = payload.get('sub')
                    # Validate email
                    # database: Session = Depends(db_handler.get_db)
                    # get_user_table(database)
                    if valid_email_from_db(email): #or valid_email_from_user_table(email)
                        # Create and return token
                        return JSONResponse({'result': True, 'access_token': create_token(email)})

    except Exception:
        raise CREDENTIALS_EXCEPTION
    raise CREDENTIALS_EXCEPTION

The reason Depends is not working is, as @MatsLindh is asking about, that you are calling router.route directly.正如@MatsLindh 所询问的那样, Depends不起作用的原因是您直接调用router.route That is the underlying method in Starlette, meaning you bypass all FastAPI specific functionality, like Depends .这是 Starlette 中的底层方法,这意味着您可以绕过所有 FastAPI 特定功能,例如Depends Rewrite it using router.get and it should work.使用router.get重写它,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 FastAPI AttributeError:“用户”对象没有“密码”属性 - FastAPI AttributeError: 'User' object has no attribute 'password' AttributeError: 'Customer' object 没有属性 'json' fastapi - AttributeError: 'Customer' object has no attribute 'json' fastapi AttributeError:“博客”object 没有属性“项目”-FastAPI - AttributeError: 'Blog' object has no attribute 'items' - FastAPI AttributeError: 'FastAPI' 对象没有属性 'logger' - AttributeError: 'FastAPI' object has no attribute 'logger' SqlModel:Fastapi AttributeError:类型 object 'AddressBaseCore' 没有属性 '__config__' - SqlModel : Fastapi AttributeError: type object 'AddressBaseCore' has no attribute '__config__' AttributeError:类型对象“ Page”没有属性“ query” - AttributeError: type object 'Page' has no attribute 'query' AttributeError: 类型对象“Post”没有属性“query” - AttributeError: type object 'Post' has no attribute 'query' AttributeError: 类型对象“用户”没有属性“查询” - AttributeError: type object 'User' has no attribute 'query' AttributeError: 'ResultPage' 对象没有属性 'query' - AttributeError: 'ResultPage' object has no attribute 'query' AttributeError:'Query'对象没有属性'key'(AppEngine) - AttributeError: 'Query' object has no attribute 'key' (AppEngine)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM