【问题标题】:Get new access token with Authorization Code OAuth2 - Using Robot framework使用授权码 OAuth2 获取新的访问令牌 - 使用机器人框架
【发布时间】:2019-05-02 11:40:32
【问题描述】:

我在使用带有 Oauth2 的机器人框架获取带有授权类型授权代码的访问令牌时遇到了一些麻烦。 我们还使用用户名/密码身份验证,在提供以下参数后,我们会取回访问令牌: 授权类型、回调 URL、身份验证 URL、访问令牌 URL、客户端 ID、客户端密钥、范围、状态。

我也尝试过 RequestsLibrary 和 ExtendedRequestsLibrary,但到目前为止没有成功。 其实不知道怎么加参数:callback url、auth url、access token url和state。

First try - using RequestsLibrary 

Get admin token
&{HEADER_TOKEN}=    Create Dictionary   Content-Type=${CONTENT_TYPE} 
 &{DATA_TOKEN}=    Create Dictionary     token_name=backend_token    grant_type=${GRANT_TYPE}   redirect_uri =${CALLBACK_URL}   auth_url=${AUTH_URL}   access_token_url=${ACCESS_TOKEN_URL}    client_id=${CLIENT_ID}    client_secret=${CLIENT_SECRET}    scope=${SCOPE}    state=${STATE}   username=${USERNAME}    ${PASSWORD}
 ${BACKEND_RESPONSE}=   RequestsLibrary.Post Request    ${BACKEND_SESSION}    /oauth/token      data=${DATA_TOKEN}     headers=${HEADER_TOKEN} 
 Log to console    ${BACKEND_RESPONSE}
 Should Be Equal As Strings  ${BACKEND_RESPONSE.status_code}  200

Second try - using ExtendedRequestsLibrary 

Get brand new admin token
    ${SESSION_RESPONSE}=    Create Password Oauth2 Session    client    ${TOKEN_URL}    ${CLIENT_ID}    ${CLIENT_SECRET}    ${USERNAME}    ${PASSWORD}    base_url=${BASE_URL}

    &{HEADER_TOKEN}=    Create Dictionary   Content-Type=${CONTENT_TYPE} 
    &{DATA_TOKEN}=    Create Dictionary     token_name=client   grant_type=${GRANT_TYPE}   callback_url=${CALLBACK_URL}   auth_url=${AUTH_URL}   access_token_url=${ACCESS_TOKEN_URL}    client_id=${CLIENT_ID}    client_secret=${CLIENT_SECRET}    scope=${SCOPE}    state=${STATE}
    ${BACKEND_RESPONSE}=   ExtendedRequestsLibrary.Post Request   client    /oauth/token      data=${DATA_TOKEN}     headers=${HEADER_TOKEN} 
    Log to console    ${BACKEND_RESPONSE}
    Should Be Equal As Strings  ${BACKEND_RESPONSE.status_code}  200
    Log to console    ${BACKEND_RESPONSE.status_code}

如果您有任何想法,请告诉我。

谢谢!

【问题讨论】:

    标签: api oauth-2.0 robotframework


    【解决方案1】:

    使用 RequestsLibrary 尝试这种方法应该可以工作:-

    Create Session   baseUri   https://xxxxxx.xx.xxx/xxx/xx      verify=True
    &{params}=  Create Dictionary   client_id=${client_id}   client_secret=${client_secret}   grant_type=${grant_type}
    &{headers}=  Create Dictionary   Content-Type=application/json
    ${resp}=  Post Request  baseUri  /oauth/token    none    none    ${params}  ${headers}
    Log to Console  ${resp.json()['access_token']}
    Status Should Be  200            ${resp}
    

    您在发布请求中将 data=${DATA_TOKEN} 作为正文传递。您需要将其作为查询参数发送。第一个参数将是别名第二个是uri 第三个是数据第四个是Json,第五个是查询参数所以在 发布请求 baseUri /oauth/token 无 无 ${params} ${headers} 你会发现第三个和第四个参数都没有。希望这有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 2022-08-16
      • 2016-12-10
      • 2018-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多