关联接口,依赖接口的用法
##############关联接口的用法
思想:在实际工作中,往往需要测试到关联接口(依赖接口),比如购买,我们需要保证用户是登录状态,所以在接口测试中需要拿到登录的token值,
import requests # 1. 请求第一个接口 从响应中提取得到access_token def get_access_token(): url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=kPoFYw85FXsnojsy5bB9hu6x&client_secret=l7SuGBkDQHkjiTPU3m6NaNddD6SCvDMC' res1 = requests.get(url) access_token = res1.json()['access_token'] print(access_token) return access_token def baidu_ocr(access_token): # 2. 实用提取到的数据 url = f'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={access_token}' data = { 'url': '//upload-images.jianshu.io/upload_images/7575721-40c847532432e852.png? imageMogr2/auto-orient/strip%7CimageView2/2/w/1240' } res2 = requests.post(url, data=data) print(res2.json()) def main(): token = get_access_token() baidu_ocr(token) if __name__ == '__main__': main()