0
点赞
收藏
分享

微信扫一扫

Python+request 登录接口reponse中token传递给其他接口使用,小示例介绍《一》

冬冬_79d4 2022-08-05 阅读 72

 

要求:

  1、调用登录login

  2、调用通过登录接口返回的reponse中的token和uuid,实现test_create_todo接口的测试

实现:

  1、login登录接口的调用,直接填写对应的URL、headers和data即可。再不需要其他参数的加入

  2、因test_create_todo此接口在登录后,因此需要用到token和uuid,由于是2个不同的方法,因此需要将使用的变量设置为全局变量。

实操作如下:

#!/usr/bin/env python
# coding=UTF-8
import requests

def login():
url = "https://***.***.com/v2/user/login" # 注:此处为本公司的host因此不方便外漏
data = {
"mobile": "12606666333",
"password": "33a7d3da476a32ac237b3f603a1be62fad00299e0d4b5a8db8d913104edec629"
}
headers = {
"version": "2.3.0",
"version-id": "235",
"device-id": "8BAFD18C-61E3-4BAF-8AB1-0BC16E567633",
"time": "1557728866628",
"channel-id": "001",
"os": "ios",
"Accept-Language": "zh-tw",
"device-name": "iPhoneX",
"User-Agent": "iBer/235 CFNetwork/976 Darwin/18.2.0",
#注:一定不能加content-type,否则报签名错误
# Content-Type: multipart/form-data; boundary=vHsPJ5s6grMzpmWoZxd3T3o6.LcUWBvUUu0gDNubaf05Ve7kv6bAkH3s_rr0AEc2D6AbEh
"sign": "a81b4379f504f330e83792ce2015e629"
}

r = requests.post(url=url, data=data, headers=headers)
#global uuid,token,version,version_id
global uuid
uuid = str(r.json()["data"]["uuid"])
global token
token = str(r.json()["data"]["token"])
version = "2.2.1"
version_id = "232"
print "登录成功,如下是reponse返回的内容"
print r.text

#print uuid,token,version,version_id

def test_create_todo():
url = "https://***.***.com/v2/todo/create"
data = {
"name": "1239",
"todo_remind_type": "0",
"cate_uuid":"86799e50d9890ade579c4ac88059a5ff",
"all_day":"1",
"todo_start":"2019-05-13",
"todo_end":"",
"type":"0",
"repeat_tyep":"0",
"c_user_uuid":""
}
headers = {
"version": "2.3.0",
"version-id": "235",
"os": "ios",
"sign": "123456",
"is-test":"1",
"uuid":uuid,
"token":token
}
print uuid
print token
r = requests.post(url=url, data=data, headers=headers)
print "%%%%%%%%%%%%%%%%%%"
print r.json()

login()
test_create_todo()

 

Python+request  登录接口reponse中token传递给其他接口使用,小示例介绍《一》_javascript

1.作者:Syw

2.本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

3.如果文中有什么错误,欢迎指出。以免更多的人被误导。

举报

相关推荐

0 条评论