很多同学在初探API时,心想单纯一个登录的接口还好不需要验证权限,可是登录后续的接口该如何执行呢,这里得先行了解 :
Cookie,Session,Token的区别 Python学习群:683380553,有大牛答疑,有资源共享!是一个非常不错的交流基地!欢迎喜欢Python的小伙伴!
在我们得口述逻辑中,是用户登录后返回一个带有用户信息得cookies,token相关信息,然后后续操作会携带这带有用户信息的cookies,token一并传进服务校验身份信息是否对应,对应则运行操作得到预期相应,当携带的是过期或者错误的cookies,token时则会返回状态码401,没验证身份信息。看一下在Python接口中是如何验证身份的,以下用到的是token机制:
拿到登录需要的相关json数据,headers请求头,一并传入requests请求中,并赋值给res,运行后会返回一个带有key为"access_token"的json数据,我们只需要获取响应值中"access_token"对应的Value即可,然后把获取的value赋值给token,如下代码:
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553926559162 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>
如今,我们已经获取了带有用户身份信息的token,在后续的接口中,我们需要把这个token放进请求头中,也就是headers;最后 return 返回一个全新的headers供后续接口调用,往后我们才case文件中,只需要初始化引入该函数即可。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553926559167 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>
引入获取token的函数,初始化。文字组织表述不好,直接上代码:
在class类中采用类unittest测试框架,采用的是setUp,即每次执行case时都执行一次该函数,以确保每个case携带都是最新的headers:token值;
在case函数数中,只需要把最新的headers与相关参数一并传进requests中即可,print打印Response即可。
<tt-image data-tteditor-tag="tteditorTag" contenteditable="false" class="syl1553926559170 ql-align-center" data-render-status="finished" data-syl-blot="image" style="box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", "Helvetica Neue", Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;"><input class="pgc-img-caption-ipt" placeholder="图片描述(最多50字)" value="" style="box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);"></tt-image>
在这,就完成了携带身份信息的API请求操作。当然实现的方式还有很多,欢迎探讨学习。
网友评论