[关闭]
@fuyb1986 2016-04-11T09:20:32.000000Z 字数 1233 阅读 500

使用 Token 做身份验证

好热闹


流程

Created with Raphaël 2.1.2HTML5HTML5ServerServerClientClientLogin: username,password{"token":user_token}set_value?key='token'&value=user_tokenget_value?key='token'user_token

登录获取 token

Method and URL Structure

Method Resource URL
POST /accounts/login/

Request Parameters


参数以 application/json 格式,在 request.body 中传递给接口

Name Description Required Default
username 用户名 YES
password 密码 YES

Request

  1. {
  2. "username":"123456780",
  3. "password":"123456",
  4. }

cURL Example

  1. $ curl -X POST --data username=123456780&password=123456 http://www.haorenao.cn/account/login/

JSON Response

验证成功返回 token, HTTP Status Code=200; 验证失败返回 detail, HTTP Status Code=400

Name Description NOTE
token User Token
non_field_errors 错误信息 HTTP Status Code=400
  1. {"token":"67bd40249f0016eb03acbbdcef08db6d1ae4e1c8"}

JQeury + CORS[1] 传递 token 到服务器接口

Example

  1. Authorization: Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b

Javascript Example

  1. $.ajax({
  2. type: 'GET',
  3. url: 'http://www.haorenao/api/exmaple/',
  4. contentType: 'text/plain',
  5. headers: {
  6. // 'Token' whith a whitspace
  7. Authorization: 'Token 9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'
  8. },
  9. success: function() {
  10. // Here's where you handle a successful response.
  11. },
  12. error: function() {
  13. // Here's where you handle an error response.
  14. }
  15. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注