frameworks - 使用 OAUTH2 授权请求 header 进行 Robot Framework api 测试

标签 frameworks robotframework oauth-2.0

我正在尝试在使用 OAUTH2 身份验证的 api 上使用 RequestsLibrary。

通过 OAUTH2 进行身份验证,并将凭据提供给/v1/authtoken 端点。 对 APÍ 的后续调用需要将 token 作为“承载”包含在 http 请求的“授权” header 中。

下面是测试用例。我收到的错误是: 第401章!=200

凭证在 jmeter 中工作正常,并返回帐户列表。但是,我无法使 RF 脚本正常工作。任何帮助将不胜感激。

在脚本中,

  1. 登录控制台${accessToken} 返回访问 token :8ETFXTZOWQLrgsMj7c_KuCEeypdj-eO1r...
  2. 登录控制台${token}返回:承载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()}

最佳答案

我也使用 OAuth 2.0 身份验证来实现我的销售人员自动化。

我的第一个答案是跳过基于客户端的身份验证并切换到基于用户名/密码的身份验证

      Get authToken by Password Authentication

RequestsLibrary.Create Session  hook    https://<url>/services/oauth2  verify=${True}
${data}=      Create Dictionary     grant_type=password     client_id=1abc    client_secret=2abc    username=test@test.com  password=keypass
${headers}=   Create Dictionary      Content-Type=application/x-www-form-urlencoded
${resp}=        RequestsLibrary.Post Request    hook    /token    data=${data}   headers=${headers}
Should Be Equal As Strings  ${resp.status_code}     200
${accessToken}=    evaluate    $resp.json().get("access_token")
Log to Console        ${accessToken}

如果您使用基于客户端或基于 Web 的身份验证,则会出现一个登录屏幕,用户将使用该登录屏幕输入用户名/密码,以授权应用程序代表其发送请求。请查看这些页面以获取更多信息,因为它们主要讨论使用刷新 token 或完全跳过用户提示。

关于frameworks - 使用 OAUTH2 授权请求 header 进行 Robot Framework api 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42007749/

相关文章:

testing - 我可以使用机器人框架进行报告而无需创建 testcase.txt 文件吗?

python-2.7 - 如何从python中的日期时间字符串中删除秒和毫秒

java - 了解 OAuth2 客户端凭据流

azure - 从守护程序应用程序访问受 oAuth2 保护的 Azure Function

ios - 在框架中使用 cocoapods

swift - 强制SDK仅支持一种语言

用于网络邮件服务的Python网络框架

python - Django - Rest Framework 多模型

python - 如何处理 Robot Framework RIDE 中的 Windows 身份验证弹出窗口?

oauth-2.0 - Bigcommerce - 如何请求授权码/访问 token