Forum Discussion
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,
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.
- ashrey6 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
- avidCoder6 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.
- ashrey6 years agoNew Contributor
Hello,
I dont have to sent client id and secret key in my api request.
the api call works fine in postman without client id and secret key.
the api call needs only grant type, user name, pwd to be send as body or header part. content type must be form-data-url encoded.i have attached the image of post man of the request reference.
Is it possible to get the access token without client key and secret as my cuctomer didnt share anything.
- ashrey6 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 years ago