How to make api call that requires login in web2py?

左心房为你撑大大i 提交于 2020-02-02 14:52:47

问题


I want to access APIs from application. Those APIs has decorator @auth.requires_login().

I am calling api from controller using

demo_app/controllers/plugin_task/task

url = request.env.http_origin + URL('api', 'bind_task')
page = urllib2.Request(url)
page.add_header('cookie', request.env.http_cookie)
response = urllib2.urlopen(page)

Demo API

api.py

@auth.requires_login()
@request.restful()
def bind_task():
    response.view = 'generic.json'
    return dict(GET=_bind_task)


def _bind_task(**get_params):
    return json.dumps({'status': '200'})

Above code gives me error : HTTPError: HTTP Error 401: UNAUTHORIZED How to solve this issue? I need to add any other headers?

Directory structure-

Demo_app
|
|-controllers
   |
   |-api.py 
   |-plugin_task.py

controller plugin_task.py calls bind_task() which is in api.py

EDIT:

Application .w2p file- Download

来源:https://stackoverflow.com/questions/33938076/how-to-make-api-call-that-requires-login-in-web2py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!