带身份验证的 Restful API [英] Restful API with authentication

查看:39
本文介绍了带身份验证的 Restful API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为我的 web2py 应用程序构建 API.

I try to built an API for my web2py application.

# -*- coding: utf-8 -*-
auth.settings.allow_basic_login = True

@auth.requires_login()
@request.restful()
def api():
    response.view = 'generic.json'

    def GET(tablename, id):
        if not tablename == 'division':
            raise HTTP(400)


        result = db(db.division.title == id).select()

        return dict(result = result)

return locals()

但是每次我尝试通过 curl 连接时,我都会得到这个答案:

But everytime I try to connect via curl I get this answer:

You are being redirected <a href=\"/my_manager/login?_next=/my_manager/api/get_all_divisions/1.json\">here</a>

当我注释掉这一行

@auth.requires_login()

everythink 工作正常.

everythink works fine.

我已经搜索了提示,但到目前为止还没有找到有关此主题的任何有用信息.

I've searched for hints, but didn't found any helpful information about this topic so far.

非常感谢任何帮助.

推荐答案

我知道这是一个很老的问题,但这个答案仍然会帮助其他可能遇到相同问题的人:

I know its a pretty old question but still this answer will help other people who might experience same issue:

好吧,在您的情况下,您使用的是需要用户登录的 @auth.requires_login().

Well in your case you are using @auth.requires_login() which needs user to be logged in.

因此,您需要在调用此 API 时发送基本身份验证凭据.

So you need to send basic auth credentials while making calls to this api.

最简单的 curl 调用可以是:

A simplest curl call can be:

curl --user abc@example.com:abc123 http://localhost:8000/app_name/default/api/table_name.json

这里:

  • abc@example.com 是用户名
  • abc123 是密码
  • app_name 是 web2py 的应用名称
  • table_name 是 db 中的表名

这篇关于带身份验证的 Restful API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆