Forum Discussion
stateng
13 years agoOccasional Visitor
I automated this last week for a SOAP API so you can probably adapt what I did. This is my first attempt at programing in java so it was interesting. This is what I used to make it all work:
To keep track of current credentials, tokens, state and customer I setup "Custom Properties" at the project level. By using the next three commands I could set and read these proeprties:
def project = com.eviware.soapui.model.support.ModelSupport.getModelItemProject( request )
String access_token = project.getPropertyValue( "access_token" );
project.setPropertyValue("token_birth",String.valueOf(token_birth));
In order to retrieve the token I used the HttpURLConnection library. The following worked for me (I am not a java programmer so if you have constructive criticism please share):
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
// connection.connect();
OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream());
out.write("client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=client_credentials");
out.close();
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine = "";
while ((inputLine = input.readLine()) != null) {
// retrieve access_token - not ideal but this works, look for XML parsing library
String token_raw = inputLine.split("access_token")[1];
access_token = token_raw.split("\"")[2];
log.info("New access_token=" + project.getPropertyValue( "client_name" ) + ":" + access_token);
project.setPropertyValue("access_token",access_token);
// retrieve expires_in - not ideal but this works, look for XML parsing library
String expires_raw = inputLine.split("expires_in")[1];
String expires_str1 = expires_raw.split(":")[1];
String expires_str2 = expires_str1.split(",")[0];
expires_in = Integer.parseInt(expires_str2);
project.setPropertyValue("expires_in",expires_str2);
// Save values
token_birth = now.getTime()/1000;
project.setPropertyValue("token_birth",String.valueOf(token_birth));
project.setPropertyValue("last_client_name",project.getPropertyValue( "client_name" ));
}
input.close();
Now it is just a matter of saving the token and placing the value in the header. I found you can place the same value in the header many times so you need to check if it exists first:
// Add the Authorization header
def headers = request.requestHeaders
x = headers.get("Authorization");
if ( x ) {
headers.remove("Authorization")
}
headers.put( "Authorization", "Bearer " + access_token );
request.requestHeaders = headers;
Hope this helps.
Mike.
To keep track of current credentials, tokens, state and customer I setup "Custom Properties" at the project level. By using the next three commands I could set and read these proeprties:
def project = com.eviware.soapui.model.support.ModelSupport.getModelItemProject( request )
String access_token = project.getPropertyValue( "access_token" );
project.setPropertyValue("token_birth",String.valueOf(token_birth));
In order to retrieve the token I used the HttpURLConnection library. The following worked for me (I am not a java programmer so if you have constructive criticism please share):
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(false);
// connection.connect();
OutputStreamWriter out = new OutputStreamWriter( connection.getOutputStream());
out.write("client_id=" + client_id + "&client_secret=" + client_secret + "&grant_type=client_credentials");
out.close();
BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine = "";
while ((inputLine = input.readLine()) != null) {
// retrieve access_token - not ideal but this works, look for XML parsing library
String token_raw = inputLine.split("access_token")[1];
access_token = token_raw.split("\"")[2];
log.info("New access_token=" + project.getPropertyValue( "client_name" ) + ":" + access_token);
project.setPropertyValue("access_token",access_token);
// retrieve expires_in - not ideal but this works, look for XML parsing library
String expires_raw = inputLine.split("expires_in")[1];
String expires_str1 = expires_raw.split(":")[1];
String expires_str2 = expires_str1.split(",")[0];
expires_in = Integer.parseInt(expires_str2);
project.setPropertyValue("expires_in",expires_str2);
// Save values
token_birth = now.getTime()/1000;
project.setPropertyValue("token_birth",String.valueOf(token_birth));
project.setPropertyValue("last_client_name",project.getPropertyValue( "client_name" ));
}
input.close();
Now it is just a matter of saving the token and placing the value in the header. I found you can place the same value in the header many times so you need to check if it exists first:
// Add the Authorization header
def headers = request.requestHeaders
x = headers.get("Authorization");
if ( x ) {
headers.remove("Authorization")
}
headers.put( "Authorization", "Bearer " + access_token );
request.requestHeaders = headers;
Hope this helps.
Mike.