MarketSquare / robotframework-requests

Robot Framework keyword library wrapper for requests

Home Page:http://marketsquare.github.io/robotframework-requests/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not able to configure token using Authorization OAuth2 in Robot framework ,Getting error status=401, reason=Unauthorized

dhingrachirag opened this issue · comments

Hi I am facing below issue:

I am facing one issue where I am trying to configure token using Authorization OAuth2 in Robot framework. I am using below code to configure token in Robot Framework:


Configure New Token
    Create Session   baseUri    ${base_url}     verify=True
    &{params}=  Create Dictionary   client_id=${client_id}   client_secret=${client_secret}   grant_type=${grant_type}
    &{headers}=  Create Dictionary   Content-Type= application/x-www-form-urlencoded
    ${resp}=  POST On Session    baseUri    /v2/oauth2/token    none    none    ${params}  ${headers}
    Log to Console  ${resp.json()['access_token']}
    Status Should Be  200            ${resp}

I am getting below error after executing above script:

POST Response : url=https://XXXX/XXX/oauth2/token   status=401, reason=Unauthorized   headers={'Content-Type': 'application/json', 'Content-Length': '64', 'X-Request-Id': '5f9fa71b0e00b5df9afa98700008ed54', 'X-RateLimit-Limit-Second': '5', 'RateLimit-Limit': '5', 'RateLimit-Remaining': '4', 'RateLimit-Reset': '1', 'X-RateLimit-Remaining-Second': '4', 'WWW-Authenticate': 'Basic realm="oauth2/client"', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Strict-Transport-Security': 'max-age=31536000 ; includeSubDomains', 'X-Frame-Options': 'DENY', 'Vary': 'Origin', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Expose-Headers': 'X-Request-Id', 'Date': 'Fri, 02 Dec 2022 07:17:52 GMT', 'Connection': 'close'}   body={"code":"401.3.10002","message":"Invalid Authorization Request"} |  
-- | --

But in Postman its executing successfully .PF below screenshot

Screen Shot 2022-12-02 at 12 58 05 PM

But I am getting successful response after executing it using postman .PFb the logs:

POST https://XXXX/XX/oauth2/token: {
  "Network": {
    "addresses": {
      "local": {
        "address": "XXXX",
        "family": "IPv4",
        "port": 50389
      },
      "remote": {
        "address": "1XXXX",
        "family": "IPv4",
        "port": 443
      }
    },
    "tls": {
      "reused": false,
      "authorized": true,
      "authorizationError": null,
      "cipher": {
        "name": "TLS_AES_256_GCM_SHA384",
        "standardName": "TLS_AES_256_GCM_SHA384",
        "version": "TLSv1/SSLv3"
      },
      "protocol": "TLSv1.3",
      "ephemeralKeyInfo": {},
      "peerCertificate": {
        "subject": {
          "country": "IE",
          "locality": "XXXXX",
          "organization": "XXXXXXX.",
          "commonName": "XXXXX",
          "alternativeNames": "XXXXXXXXX"
        },
        "issuer": {
          "country": "US",
          "organization": "XXXXX",
          "commonName": "XXXXXXX"
        },
        "validFrom": "May  1 00:00:00 2022 GMT",
        "validTo": "May  2 23:59:59 2023 GMT",
        "fingerprint": "FB:F3:16:7F:4F:51:C3:43:CE:66:C9:73:D1:DE:1E:51:90:11:AD:96",
        "serialNumber": "024ce159afc9395dde9e9156be778842"
      }
    }
  },
  "Request Headers": {
    "content-type": "application/x-www-form-urlencoded",
    "authorization": "Basic NzhhNzU1YzktNTlkMi0xMWVkLWE5NzEtNjkyY2VjYmVkZTQ4OmVOUE1xR2FjVlRxWTJFV1UvVFJKekRmR2VmRFk1NWpGTkI0a2RkSStRQUU9",
    "user-agent": "PostmanRuntime/7.29.2",
    "accept": "*/*",
    "cache-control": "no-cache",
    "postman-token": "90a68949-9002-4b07-983e-afcf3568de1b",
    "host": "XXXXXXX",
    "accept-encoding": "gzip, deflate, br",
    "connection": "keep-alive",
    "content-length": "29"
  },
  "Request Body": {
    "grant_type": "client_credentials"
  },
  "Response Headers": {
    "content-type": "application/json;charset=UTF-8",
    "x-request-id": "b2973e63fb03c0624b67f3de361ea074",
    "ratelimit-limit": "5",
    "ratelimit-remaining": "4",
    "ratelimit-reset": "1",
    "x-ratelimit-remaining-second": "4",
    "x-ratelimit-limit-second": "5",
    "x-asavie-tenant-id": "a529ca70-1685-45bd-83a8-f61815d86130",
    "cache-control": "no-store",
    "pragma": "no-cache",
    "x-content-type-options": "nosniff",
    "x-xss-protection": "1; mode=block",
    "strict-transport-security": "max-age=31536000 ; includeSubDomains",
    "x-frame-options": "DENY",
    "vary": "Origin",
    "access-control-allow-credentials": "true",
    "access-control-expose-headers": "X-Request-Id",
    "content-length": "1344",
    "date": "Fri, 02 Dec 2022 07:29:53 GMT",
    "connection": "keep-alive",
    "set-cookie": "ak_bmsc=642B70ABECE79B02A0798416507A53CE~000000000000000000000000000000~YAAQdfhWuOY4GYSEAQAAiOK90RFsvFobcwcoNDqKqbOm5JM2CBnUSgrqNlEOUvpqafqGEoWC1Yf26HnQK6JjIgCria4ersDHzU4sJ55XnGIhUUdtSkU5ROh814JnIlFYdLeBPDdyNT9aVPyyTnnXoazICQnl0SEHfIrnPJCGTNdo/yLIEFmHLxpcu75VyuilDPKFpPxPxz/R0h4wMtNDQdEZ2k3jiXA9QlsMXZCHD9Dokb7Ynh4ki9brBKFMDhGc1I5VpXQL/J8j5KNtLErqrXHol16PQVSI/RU1PMgey/ZN9Gyw/ir8GR19idixuGfDA9D28uFBDGlvMqMXfowoJKo1HAuNyHJYO5SAm22rtWAw5lKb+0EEjrH+TcNlKfUz; Domain=.securemobi.net; Path=/; Expires=Fri, 02 Dec 2022 09:29:52 GMT; Max-Age=7199; HttpOnly"
  },
  "Response Body": "{\"access_token\":\"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Imp3ay1rZXktaWQifQ.eyJlbWFpbHMiOlsic2hqb3NoQGFrYW1haS5jb20iXSwiYXVkIjpbIklEUCJdLCJvd25lcl9pZCI6ImE1MjljYTcwLTE2ODUtNDViZC04M2E4LWY2MTgxNWQ4NjEzMCIsInNjb3BlIjpbIlRlbmFudF9NYW5hZ2VyIl0sIm9pZCI6Ijc4YTc1NWM5LTU5ZDItMTFlZC1hOTcxLTY5MmNlY2JlZGU0OCIsImV4cCI6MTY2OTk2OTc5MywiYXV0aG9yaXRpZXMiOlsiSUFNIl0sImp0aSI6IjgyYTE1ODUwLTNlMGItNDA0Zi04ZDg0LTZkMmRmMjBiNjdjNyIsImlzc3VlciI6Ind3dy5hc2F2aWUuY29tIiwib3duZXJfdHlwZSI6InRlbmFudCIsImNsaWVudF9pZCI6Ijc4YTc1NWM5LTU5ZDItMTFlZC1hOTcxLTY5MmNlY2JlZGU0OCJ9.oautqMmW7_CqFgxoCOLFJSLejCANkh5PGr9xDcot3j5J3R2J8ewLw0kqkZ76hqSaAQVTRAhPTx897dqOGY-j8sCi17gSrlQ2hzdWMCIjwk43-HpRSXJzP7jFbbzhdG74iDRH22z5iFTNZr-F4eeo9I4OoqzXUZOVqzTZE_jbzjR-pGiY34oCli-acMEOQV05r0EgnOnPP76yIXj3BAGJ_Qnb3Zm6vbd-XraL6ew15Di2g9CtAbde-sldufbrZcBH58dK4wQQqZio-ZsWplnwP7HJdCs8lM3mmzPvR3mtr6SpXNjks9h93jN5zuh3WXXyB_jLfS5-b_KWrD70DWngFVOBzATvjdkIdFTJxzcQyd2-o-jEL2S8YrYsWpWgIiFAysoPmAyN-y2Bxs7tm8U1eITZkgtIRC5b8pCdlE6_RPHZxzg_cxsG2oidsj5J30qxVde84w8S6rfvkKi5nBkTteH4ZsvRBM7uYeQjl0SOHbCxOoE_WUqQTMGw62H6Y9c5NGPZQt6e2abF7WXF_fEezc3GMQoPKGOmwCtFJkeuNOLoJjp79ZvZFfphnAMCG3wHpfvQ8EeUf9_-XeRzbawsmfDfTt2uqrOWo2QM_2aF8yxP00M5cWahCjuK-fgiJFWWE5cQcNeWqGQaaKIRSgIYOEkP4ulOhRu754sKsW-PDVk\",\"token_type\":\"bearer\",\"expires_in\":3599,\"scope\":\"Tenant_Manager\",\"jti\":\"82a15850-3e0b-404f-8d84-6d2df20b67c7\"}"
}

Requesting you to please assist on this issue.

It seems the argument positions are not correct.
I suggest you to specify them like this:

    ${resp}=  POST On Session    baseUri    /v2/oauth2/token  data=${params}  headers=${headers}

@lucagiove - I tried with the above suggested solution.But I got below error:

HTTPError: 401 Client Error: Unauthorized for url: https://xxxxx/xx/oauth2/token

POST Response : url=https://xxxx/xx/oauth2/token   status=401, reason=Unauthorized   headers={'Content-Type': 'application/json', 'X-Request-Id': 'df0a7a32c54df575d20cf5664b331f38', 'RateLimit-Limit': '5', 'RateLimit-Remaining': '4', 'RateLimit-Reset': '1', 'X-RateLimit-Remaining-Second': '4', 'X-RateLimit-Limit-Second': '5', 'WWW-Authenticate': 'Basic realm="oauth2/client"', 'X-Content-Type-Options': 'nosniff', 'X-XSS-Protection': '1; mode=block', 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 'Pragma': 'no-cache', 'Expires': '0', 'Strict-Transport-Security': 'max-age=31536000 ; includeSubDomains', 'X-Frame-Options': 'DENY', 'Vary': 'Origin', 'Access-Control-Allow-Credentials': 'true', 'Access-Control-Expose-Headers': 'X-Request-Id', 'Content-Length': '53', 'Date': 'Mon, 05 Dec 2022 09:45:27 GMT', 'Connection': 'close'}   body={"code":"401.3.10001","message":"Bad Request Format"} |  
-- | --

But in postman its working fine.