Sandor Nagy
Sandor Nagy

Reputation: 21

Get new access token with Authorization Code OAuth2 - Using Robot framework

I have some troubles with getting Access token with grant type authorization code using Robot framework with Oauth2. We use also a username/password authentication and after give the following parameters we get back our access token: Grant Type, Callback URL, Auth URL, Access Token URL, Client ID, Client Secret, Scope, State.

I tried with RequestsLibrary and ExtendedRequestsLibrary as well, but no success so far. Actually I do not know how to add parameters: callback url, auth url, access token url and 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}

If you have any idea just let me know.

thx!

Upvotes: 2

Views: 3266

Answers (1)

Simranpal Singh
Simranpal Singh

Reputation: 11

using RequestsLibrary try with this approach it should work:-

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}

you are passing data=${DATA_TOKEN} as a body in your post request. You need to send it as query params. First parameter will be alias 2nd is uri 3rd is data 4th is Json and 5th is query params so in Post Request baseUri /oauth/token none none ${params} ${headers} you will find 3rd and 4th parameter as none. Hope this works

Upvotes: 1

Related Questions