首页 > 解决方案 > 如何使用机器人测试框架从创建的会话中获取访问令牌

问题描述

嗨,这是我的脚本文件。我想从创建的会话中获取访问令牌。我尝试过这种方式,但我得到的访问令牌没有。任何帮助我。

    Create Session  httpbin     ${server_URL}       verify=${True}
&{dict}    Create Dictionary        username=${username}    password=${password}   email=${email}       role=${role}    
${header}  Create Dictionary  Content-Type=application/json    charset=UTF-8     
${resp}     Post Request    httpbin     /api/Owners          data=${dict}    headers=${header}
${accessToken}=    evaluate    $resp.json().get("access_token")
Log to Console        ${accessToken}
log Many        ${resp.text}
${id}   Evaluate     $resp.json().get("id")
Log     ${id}
log to console      ${resp.status_code}
${resp}  Get Request     httpbin        uri=/api/Owners?access_token=${accessToken}
${resp}     Get Substring   ${resp.text}    1   -1
Log      ${resp}
${json_str}    Convert JSON To String    ${resp}
Should Contain      ${json_str}         ${id}

标签: python-2.7

解决方案


使用请求我将尝试登录并创建一个令牌

import requests

login = {
      "user" : "username",
      "pass" : "passwrod",
}

这里写你自己的令牌,这只是一个例子

accesToken = {

"Accept" : "application/json",

}

request = requests.post('http://www.web.com/user', data=login, headers=accesToken')

推荐阅读