Forum Discussion
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.
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.
- avidCoder6 years agoSuper Contributor
I just referred the picture you attached, I guess you should mention grant_type as client_credentials if you have client_id and client_secret. You cannot fetch the access token without providing the client_id and client_secret for the grant_type as client_credentials to the best of my knowledge. Please refer this link for your understanding:-
- ashrey6 years agoNew Contributor
Hello,
Thank you for the information.
I dont have the client id and client secret. the ready api is executable with the grant type password.
i also referred the docs from here :
https://docs.apigee.com/api-platform/security/oauth/implementing-password-grant-type
Also i tried to execute your code whihc you have shared, but facing an error. added the imports and jar.
This is the line of code showing the error : builder.loadTrustMaterial( trustStrategy);
The method loadTrustMaterial(org.apache.http.ssl.TrustStrategy) in the type SSLContextBuilder is not applicable for the arguments (org.apache.http.conn.ssl.TrustStrategy)
Is it due to version of jar/java?
Thanks for your support.
- avidCoder6 years agoSuper Contributor
Use java_1.8.0_181 version.
Related Content
- 2 years ago