Rest request oAuth2 token POST

I want to utilize oAuth2 for a token POST request. I do not see any native support for this authorization format. I think a custom keyword may be the way but I need advice. I have created in the Object Repository a Token Post request, and it works, when I manually add an Authorization in HTTP Headers ( contains realm, basic logon parameters, timestamp and nonce…) and it returns the Token. My question is, How can I execute a groovy script within a custom keyword to generate the Authorization values and post them to the Authorization field in the HTTP Headers of my Object (Token_oAuth in the Repository ) or could I run a script inline of the test case to generate the Authorization string values and then add them to the Token_oAuth Object as it is called perhaps as a parameter ?

 

Any advice is appreciated,

Mark

Thank you for your response, however The name of my Web Service request has changed to POST because I recreated each portion and verified their functionality prior to responding . The web Service does function as expected when configured with hard coded values (set in the web service) my issue is setting the value of the HTTP Headers ‘Authorization’ from the script within the test case.

So using the example above the line “header.setValue(‘Basic realm=MyPage.NET&grant_type=password&username=user@tran.net&password=password&timestamp=1486664262&nonce=CgCHNY&client_id=abcef0123456789’)” is failing to set the value. I also note that failure does not throw an error or exception. I have tried passing both variables with assigned strings, and as shown a hard coded string, with no change. The value is not assigned to the Authorization header in the Web service request.

The reason of your failure is

responseData = WS.sendRequest(findTestObject(‘POST’))

You need to use as

responseData = WS.sendRequest(tokenOAuth)

I have tried both options, but with no success. Neither adding a string value to the preexisting HTTP header ‘Authorization’ or adding the Header value ‘Authorization’ + string value worked for me. I am using Katalon Studio Version: 4.4.0 Build: 1.

When I run the test I am returned a 400 error, and when i open the WebServiceRequest I see the property has not value.

I am using this script. the Web service request is called POST (its an oAuth2 POST request, I am wanting to add a string value to the HTTP Header, ‘Authorization’)

RequestObject tokenOAuth = findTestObject(‘POST’)

for (TestObjectProperty header : tokenOAuth.getHttpHeaderProperties()) {
if (header.getName().equals(‘Authorization’)) {
header.setValue(‘Basic realm=MyPage.NET&grant_type=password&username=user@tran.net&password=password&timestamp=1486664262&nonce=CgCHNY&client_id=abcef0123456789’)

break
}
}

responseData = WS.sendRequest(findTestObject(‘POST’))
responseCode = responseData.getStatusCode()
println('Response Code: ’ + responseCode)

responseText = responseData.getResponseText()
println('Response body: ’ + responseText)

It does appear the even though no error is thrown, I cannot add a value to the HTTP Headers ‘Authorization’ or add a header as in either of your examples. Am I missing something environmental?

Thanks,

Mark

Hi Mark,

You could

run a script inline of the test case to generate the Authorization string values and then add them to the Token_oAuth Object as it is called perhaps as a parameter

Example:

// need to import com.kms.katalon.core.testobject.RequestObject// need to import com.kms.katalon.core.testobject.TestObjectPropertyRequestObject tokenOAuth = findTestObject('Your Token_oAuth ID')// Option 1: tokenOAuth already has Authorization header valuefor (TestObjectProperty header : tokenOAuth.getHttpHeaderProperties()) {    if (header.getName().equals('Authorization')) {        header.setValue('Your new token value')        break    }}// Option 2: tokenOAuth has none Authorization header value// need to import com.kms.katalon.core.testobject.ConditionTypetokenOAuth.getHttpHeaderProperties().add(new TestObjectProperty('Authorization', ConditionType.EQUALS, 'Your token value'))

You can refer to the test case: Test Cases/Web API/JIRA REST API/TC02_CreateUser_AddUserDataSuccessfully in the following project to resolve your issue: https://drive.google.com/file/d/0B90U2dv73Mz5aFFJdVFGMFlWdU0/view?usp=sharing

The given solution in that test case is to add custom fields into HTTP headers:

requestUser.getHttpHeaderProperties().add(CustomKeywords.‘com.example.WebApiCustomKeywords.createBasicAuthProperty’(P_UserAdmin, P_PassAdmin))