requests的token接口

token接口
案例:网站:http://shop.duoceshi.com/login?redirect=%2Fdashboard

image
code接口:http://manage.duoceshi.com/auth/code
image
image

登录接口:http://manage.duoceshi.com/auth/login
image
登录接口参数:{"username":"admin","password":"e6WGOz+g/FuR646O7IF8JrlC6qH/anCI9/0UCsVDnUxN2aBdGKtRffNb1W7i87dRavZCNyP9yqvAcXLgdKtsRA","code":"8888","uuid":"{{uuid}}"}
image
链接类型:Content-Type:application/json
image
image
搜索商品接口:http://manage.duoceshi.com/api/yxStoreCategory?page=0&size=10&sort=sort%2Cdesc
image
Authorization:
Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImV4cCI6MTY4MDE0OTgwNX0.fmlEdR5Xh1BF2al6crfPMjG8dd-M6TOKux3wni5ejvGFLce4Zk0tvKsCobj7WEaXvXI-N-21AIj2HH5LFlFMNw
image
image
案例:
import  requests
s=requests.Session()
class  tk(object):
def init(self):
pass
    def uid(self):
        url1="http://shop.duoceshi.com/apis/auth/code"
headers={"content-type":"application/json"}
        d=s.get(url=url1,headers=headers)
print(d.text)
self.js=d.json()["uuid"] # 将响应体转换成json格式,通过uuid接受
print(self.js)
def login(self):
        url2="http://shop.duoceshi.com/apis/auth/login"
data2={
"username": "admin",
"password": "bcguNVePpEv/7/4AiIAIubR3F43NPQbUXUBnkYTCY2EF6cZbiDQ3qwxBTvjM7WegTguBQyqEaLumyh6+bR3Xyw
",
"code": "8888",
"uuid": self.js}
        headers2 = {"content-type": "application/json"}
        d=s.post(url=url2,json=data2,headers=headers2)
print(d.text)
self.js1=d.json()["token"]
print(self.js1)
def buid(self):
        url3="http://shop.duoceshi.com/apis/api/menus/build"
headers3 = {"content-type": "application/json",
"authorization":self.js1}
        dx=s.get(url=url3,headers=headers3)
print(dx.text)
if name == 'main':
    dx=tk()
    dx.uid()
    dx.login()
    dx.buid()


re正则方法提取:
import re
s=requests.Session()
class  tk(object):
def init(self):
pass
    def uid(self):
        url1="http://shop.duoceshi.com/apis/auth/code"
headers={"content-type":"application/json"}
        d=s.get(url=url1,headers=headers)
print(d.text)
self.tq1="".join(re.findall('"uuid":"(.+?)"',d.text))
print(self.tq1)
def login(self):
        url2="http://shop.duoceshi.com/apis/auth/login"
data2={
"username": "admin",
"password": "bcguNVePpEv/7/4AiIAIubR3F43NPQbUXUBnkYTCY2EF6cZbiDQ3qwxBTvjM7WegTguBQyqEaLumyh6+bR3Xyw==",
"code": "8888",
"uuid": self.tq1}
        headers2 = {"content-type": "application/json"}
        d=s.post(url=url2,json=data2,headers=headers2)
print(d.text)
self.tq2=''.join(re.findall('"token":"(.+?)"',d.text))
print(self.tq2)
def buid(self):
        url3="http://shop.duoceshi.com/apis/api/menus/build"
headers3 = {"content-type": "application/json",
"authorization":self.tq2}
        dx=s.get(url=url3,headers=headers3)
print(dx.text)
if name == 'main':
    dx=tk()
    dx.uid()
    dx.login()
    dx.buid()

posted @ 2026-05-25 15:40  刘sir金牌讲师  阅读(11)  评论(0)    收藏  举报