Sync call with Oauth2 needed in Testcomplete
- 4 years ago
Hi Alex,
Thanks for your response. Eventually we found a solution. I'll include my code in this ticket for others to use.
It was not an easy journey to find this.
To respond to your question: we use Testcomplete to do web UI testing, but occasionally we need to do some API calls to make our live a bit easier.(mostly to do 'shorter steps' where if scripted to do it by UI, too heavy or not relevant for the running test).
function httpPostRequestBearer() { var username = "<username>"; var password = "<password>"; var keycloakurl = "https://<authentication client URL>"; const params = { username: username, password: password, client_id: '<cliend-id>', client_secret: '<client-secret>', grant_type: 'password' }; const data = Object.keys(params) .map((key) => `${key}=${encodeURIComponent(params[key])}`) .join('&'); var tempBody = data // Define the request body JSON string var requestBody = tempBody; // Create the aqHttpRequest object var aqHttpRequest = aqHttp.CreatePostRequest(keycloakurl); // Specify the Content-Type header value aqHttpRequest.SetHeader("Content-Type","application/x-www-form-urlencoded"); // Send the request, create the aqHttpResponse object var aqHttpResponse = aqHttpRequest.Send(requestBody) var token = JSON.parse(aqHttpResponse.Text).access_token return token; } function httpPostRequest(param,bearertoken) { var address = "https://<url for service needing a token>"; var tempBody = '{ <your body possibly inserting your param from argumentlist> }' // Define the request body JSON string var requestBody = tempBody; // Create the aqHttpRequest object var aqHttpRequest = aqHttp.CreatePostRequest(address); // Specify the Content-Type header value aqHttpRequest.SetHeader("Content-Type", "application/json"); aqHttpRequest.SetHeader("Authorization", "bearer "+bearertoken); // Send the request, create the aqHttpResponse object var aqHttpResponse = aqHttpRequest.Send(requestBody) if (aqHttpResponse != null) { // Read the response data Log.Message(aqHttpResponse.AllHeaders); // All headers Log.Message(aqHttpResponse.GetHeader("Content-Type")); // A specific header Log.Message(aqHttpResponse.StatusCode); // A status code Log.Message(aqHttpResponse.StatusText); // A status text Log.Message(aqHttpResponse.Text); // A response body var JSONobj = JSON.parse(aqHttpResponse.Text); } return JSONobj }
The code may not be the 'cleanest' but it does the job. The first function will get the bearer token and the second function is a POST call using the bearer token from the first passed by as an argument. The code has been cleaned to not have my exact calls of course, but it will provide an idea how we tackled it.
Hope someone can enjoy it later too. 😊