Python接口自动化基础---token鉴权

有些登录使用cookie,有些登录需要token验证,token传参一般有两种形式,一种是在请求头中,一种是使用URL传参

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

#登录
param1={'username':'xxx','password':'xxxx'}
r1=requests.post('http://127.0.0.1:3000/login',data=param1)
print(r1.text)
print(r1.status_code)
#获取登录后token
dic1=json.loads(r1.text)
token1=dic1['token']
print(token1)
#token添加到请求头中,访问
header={'Authorization':'Bearer '+token1}

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

#结果
访问成功

 

posted @ 2017-10-08 15:39  tangqiu  阅读(8914)  评论(0编辑  收藏  举报