token鉴权

有些登录使用cookie,有些登录需要token验证。

token传参一般有两种形式,一种是在请求头中,一种是使用URL传参。

这里举例说明一下请求头中的token方式:


#登录

param1={'username':'xxx','password':'xxxx'}

r1=requests.post('http://127.0.0.1:12306/login',data=param1)

print(r1.text)

print(r1.status_code)


#获取登录后token

扫描二维码关注公众号,回复: 7174812 查看本文章

dic1=json.loads(r1.text) 或者直接 dict1=r1.json()

token1=dic1['token']

print(token1)


#token添加到请求头中,访问

header={'Authorization':'Bearer '+token1}

r2=requests.get('http://127.0.0.1:12306/api/tasks',headers=header)


猜你喜欢

转载自blog.51cto.com/11009785/2435340