【发布时间】:2019-07-23 15:47:35
【问题描述】:
我正在尝试在使用 OAUTH2 身份验证的 api 上使用 RequestsLibrary。
通过 OAUTH2 进行身份验证,并将凭据提供给 /v1/authtoken 端点。 对 APÍ 的后续调用需要将令牌作为“承载”包含在 http 请求的“授权”标头中。
所以下面是测试用例。我得到的错误是: 401 != 200
凭证在 jmeter 中工作正常,并返回帐户列表。但是,我无法让 RF 脚本工作。任何帮助将不胜感激。
在脚本中,
- 登录到控制台
${accessToken}返回访问令牌:8ETFXTZOWQLrgsMj7c_KuCEeypdj-eO1r... - 登录到控制台
${token}返回:Bearer 8ETFXTZOWQLrgsMj7c_KuCEeypdj-eO1r...
*** Test Cases ***
Get authToken
Create Session hook http://xxxx.azurewebsites.net verify=${True}
${data}= Create Dictionary grant_type=client_credentials client_id=yyy-zzzz client_secret=xxxxxxxxxxxxxxx
${headers}= Create Dictionary Content-Type=application/x-www-form-urlencoded
${resp}= post request hook /v1/authtoken data=${data} headers=${headers}
Should Be Equal As Strings ${resp.status_code} 200
Dictionary Should Contain Value ${resp.json()} bearer
${accessToken}= evaluate $resp.json().get("access_token")
Log to Console ${accessToken}
${Bearer}= Set Variable Bearer
${token}= catenate Bearer ${accessToken}
Log to Console ${token}
${headers}= Create Dictionary Authorization=${token}
${resp1}= get request hook /v1/integration/accounts headers=${headers}
Should Be Equal As Strings ${resp1.status_code} 200
#Log to Console ${resp1.json()}
【问题讨论】:
-
@sandeep @Bryan 您是否找到了问题的答案,因为我遇到了同样的错误。请分享答案
-
抱歉,我不在。刚看到这个讨论......我会试试这个。
-
是的,这似乎对我有用。谢谢。但是,我生成auth2令牌的纯python方法是,我必须说得更简洁!
标签: frameworks robotframework oauth2