python接口自动化-token登录

1. 带token的请求

import requests
import warnings
warnings.filterwarnings('ignore')

host = 'https://buyerapi.chinagoods.com/v1/auth/login'
body = {"grant_type": "password",
        "scope":  "buyer",
        "username": "17805202241",
        "password": "1234qwer"
        }
r = requests.post(url=host, json=body, verify=False)
print(r.status_code)
token = r.json()['access_token']
print(token)
header = {'Content-Type': 'application/json'}
Authorization = 'bearer' + ' ' + token
header['Authorization'] = Authorization
url_1 = 'https://buyerapi.chinagoods.com/v1/member/user/info'
r_user_info = requests.get(url_1, headers=header, verify=False)
print(header)
print(r_user_info.status_code)
print(r_user_info.text)

 其实就是登录后,将token提取出来,然后放到header中就可以了

原文地址:https://www.cnblogs.com/yuer02/p/14647904.html