使用授权码OAuth2获取新的访问令牌-使用机器人框架

时间:2019-05-02 11:40:33

标签: api oauth-2.0 robotframework

使用带Oauth2的Robot框架获取带有授予类型授权码的访问令牌时遇到一些麻烦。 我们还使用用户名/密码身份验证,并在输入以下参数后返回访问令牌: 授予类型,回调URL,身份验证URL,访问令牌URL,客户端ID,客户端密钥,范围,状态。

我也尝试了RequestsLibrary和ExtendedRequestsLibrary,但到目前为止没有成功。 实际上,我不知道如何添加参数:回调URL,身份验证URL,访问令牌URL和状态。

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}

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

thx!

1 个答案:

答案 0 :(得分: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}作为帖子请求中的正文。您需要将其作为查询参数发送。第一个参数将是别名2nd是uri 3rd是数据4th是Json,5th是查询参数 发布请求baseUri / oauth / token无无$ {params} $ {headers} 您会发现第3个和第4个参数均为none。希望这行得通