Forum Discussion
Hello avidCoder,
The easiest way to get an access token programmarically in ReadyAPI is:
1. Configure the OAuth 2.0 profile for a request and make sure that you can retrieve the token manually.
2. If the selected flow requires some interaction with the internal browser, add the corresponding JavaScript code to the "Automation..." section to automate this interaction.
3. Add the Groovy code which accesses the configured OAuth profile and runs the token retrieval method.
Step #2 and #3 are described in details in this article: https://support.smartbear.com/readyapi/docs/projects/requests/auth/types/oauth2/automate/sample.html
Hi all,
NBorovykh, thank you for sharing your expertise!
avidCoder do the above steps work for you? Or, do you have any additional information for us?
We are looking forward to hearing from you,
- avidCoder7 years agoSuper Contributor
Yes, I found the solution for my query. I used springframework code to implement this. And it was just 6-7 lines of code. Thanks for the support.
- ashrey7 years agoNew Contributor
Hello,
Am also looking for a solution to implement the access token.
I have the rest api which returns the access token. now i want to call that rest call in java inorder to get the access token, by passing grant type, username and password.
https://example.com/oauth2/token is the uri.
Coud you please share the piece of code or help me out with this how to achieve.
Thanks for your support,
Ashrey
- avidCoder7 years agoSuper Contributor
Hi Ashrey,
Please try below code to fecth the AuthToken and replace CLIENT-ID, CLIENT-SECRET and AUTHTOKEN-URL with your value:-
import java.io.IOException; import java.io.InputStream; import java.security.KeyManagementException; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContextBuilder; import com.google.gson.JsonElement; import com.google.gson.JsonParser; import com.mashape.unirest.http.HttpResponse; import com.mashape.unirest.http.Unirest; public String getAuthToken() { try { TrustStrategy trustStrategy = new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }; SSLConnectionSocketFactory sslsf = null; try { SSLContextBuilder builder = new SSLContextBuilder(); builder.loadTrustMaterial(trustStrategy); sslsf = new SSLConnectionSocketFactory( builder.build()); CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory( sslsf).build(); } catch (NoSuchAlgorithmException | KeyManagementException e) { } catch (KeyStoreException e) { e.printStackTrace(); } CloseableHttpClient client = HttpClients.custom().setSSLSocketFactory(sslsf).build(); Unirest.setHttpClient(client); HttpResponse<String> response = Unirest.post("https://CLIENT-ID:CLIENT-SECRET@AUTHTOKEN-URL?grant_type=client_credentials") .header("cache-control", "no-cache") .asString(); JsonElement je = new JsonParser().parse(response.getBody()); return je.getAsJsonObject().get("access_token").getAsString().toString(); } catch (Exception e) { e.printStackTrace(); return null; } }
Let me know if you face any issue and Accept as a solution if it works for you.
- ashrey7 years agoNew Contributor
Hello,
Can you please help me how to acheive .
I want to get access token from the rest api to access the resources from server. using grant type, user name, paasowrd. tried few methods but not successful.
Related Content
- 2 months ago
Recent Discussions
- 15 hours ago
- 17 days ago