1

I am trying to get access token from https://login.microsoftonline.com/{tenentname}/oauth2/v2.0/token endpoints though HttpClient post request. Applied required 4 parameters/headers with the same. But I am getting connection timed out. Connection will be retried using another IP address (after trying with 7 different IP address) getting Shutdown connection error. Connection discarded.

Please find below code snippet.

HttpClientConnectionManager poolingConnManager = new PoolingHttpClientConnectionManager();    
HttpPost post = new HttpPost("https://login.microsoftonline.com/{tenentname}/oauth2/v2.0/token"); 
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("grant_type", "client_credentials"));
urlParameters.add(new BasicNameValuePair("client_id", {id_value}));
urlParameters.add(new BasicNameValuePair("client_secret", {secret_value}));
urlParameters.add(new BasicNameValuePair("scope", "https://graph.microsoft.com/.default"));

post.setEntity(new UrlEncodedFormEntity(urlParameters));
post.setHeader("Content-type", "application/x-www-form-urlencoded");

ClosableHttpClinet httpClient = HttpClients.custom().setConnectionManager(poolingConnManager).build();
ClosableHttpResponse response = httpClinet.execute(post);
System.out.println(EntityUtils.toString(response.getEntity()));

Same its worked with Postman without any proxy setting, I am getting proper response in Postman. Please assist on the same.

  • Instead of using HTTPClient, i tried with Microsoft Graph Client (you can download from NuGet) and it worked for me. So i would suggest you to follow the steps. (1) [Install the SDK](https://learn.microsoft.com/en-us/graph/sdks/sdk-installation) (2) [Configure the GraphClient](https://learn.microsoft.com/en-us/graph/sdks/create-client?tabs=CS) (3) [Make the call](https://learn.microsoft.com/en-us/graph/sdks/create-requests?tabs=CS). Hope this helps you moving forward. – Dev Dec 23 '20 at 10:24
  • Thanks for the reply, could you please tell me what will be dependency for ivy as we are using ivy.xml not maven or gradle. – Anand Suryavanshi Dec 23 '20 at 11:35
  • The closest one i can find is [this](https://jar-download.com/artifacts/com.microsoft.azure/spring-social-microsoft-graph/0.1.5/source-code/com/microsoft/azure/msgraph/api/Messages.java) – Dev Dec 24 '20 at 09:23
  • Does it helped? – Dev Dec 24 '20 at 18:37
  • Hi Dev, yes it worked thanks – Anand Suryavanshi Dec 24 '20 at 19:25
  • Glad that it helped. Thanks for confirming :) – Dev Dec 25 '20 at 04:38
  • Moving this to answer. Consider upvoting and accepting it as answer. So it will be useful to the others in the community as well. – Dev Dec 25 '20 at 04:39

1 Answers1

0

Instead of using HTTPClient, i tried with Microsoft Graph Client (you can download from NuGet) and it worked for me. So i would suggest you to follow the steps.

Also with specific to ivy dependency, here's the related thread.

Dev
  • 2,428
  • 2
  • 14
  • 15
  • Thanks Dev. Which jar it required like we have Microsoft graph Auth, Core or Beta. Could you please tell me the specific jar and its version as I am not getting the ClientCredentialProvider and IGraphServiceClient from microsoft-graph-2.4.0.jar. – Anand Suryavanshi Jan 06 '21 at 13:46