5

I am using MS graph Java sdk in order to access O365 ODFB drives from my java application. When I am by-passing the enterprise proxy server(connected to open internet) then I am able to get desired response from the APIs. But when I am connecting via proxy server, I am getting the following exception:

SEVERE: Throwable detail: com.microsoft.graph.core.ClientException: Error during http request
Exception in thread "main" com.microsoft.graph.core.ClientException: Error during http request
    at com.microsoft.graph.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:356)
    at com.microsoft.graph.http.DefaultHttpProvider.send(DefaultHttpProvider.java:197)
    at com.microsoft.graph.http.DefaultHttpProvider.send(DefaultHttpProvider.java:177)
    at com.microsoft.graph.http.BaseCollectionRequest.send(BaseCollectionRequest.java:87)
    at com.microsoft.graph.requests.generated.BaseDriveItemCollectionRequest.get(BaseDriveItemCollectionRequest.java:54)
    at com.example.TestMain.main(TestMain.java:148)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at sun.security.ssl.BaseSSLSocketImpl.connect(Unknown Source)
    at sun.net.NetworkClient.doConnect(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.http.HttpClient.openServer(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.<init>(Unknown Source)
    at sun.net.www.protocol.https.HttpsClient.New(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at java.net.HttpURLConnection.getResponseCode(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
    at com.microsoft.graph.http.UrlConnection.getResponseCode(UrlConnection.java:110)
    at com.microsoft.graph.http.DefaultHttpProvider.sendRequestInternal(DefaultHttpProvider.java:297)

The code that I am using to connect Graph API is as follows:

// Get an access token for the client
    Authenticator authenticator = new Authenticator();
    IAuthenticationProvider authenticationProvider = authenticator.getAuthenticationProvider();

    graphClient = GraphServiceClient
            .builder()
            .authenticationProvider(authenticationProvider)
            .buildClient();

    graphClient.getLogger().setLoggingLevel(LoggerLevel.DEBUG);

Getting error at this line:

DriveItem item = graphClient.users().byId("userPrincipalName")
        .drive()
        .root()
        .children()
        .buildRequest()
        .get()
        .getCurrentPage()
        .get(0);

I am able to get the bearer token by passing the proxy configuration to the HttpURLConnection.openConnection(proxy) method inside Authenticator class.

e.g.:

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("myproxyaddress", 80));
@SuppressWarnings("restriction")
String encoded = new sun.misc.BASE64Encoder().encodeBuffer(("username:password").getBytes()).replace("\r\n", "");
URL url = new URL(tokenEndpoint);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy);
conn.setRequestProperty("Proxy-Authorization", "Basic " + encoded);

Am I missing out anything? Please help.

abhishek
  • 105
  • 3
  • 9
  • Does your enterprise proxy require authentication? There is a related post here https://stackoverflow.com/questions/1432961/how-do-i-make-httpurlconnection-use-a-proxy Could you provide a little more of your setup code to get an idea of how this is being configured? – Darrel Miller Oct 31 '18 at 13:08
  • 1
    @Darrel Yes, my proxy server requires authentication. I have updated my question with the setup code. – abhishek Nov 01 '18 at 06:03
  • Any solution for this question? Thanks – Rafa Hernández Sep 03 '19 at 14:29
  • Possible duplicate of https://stackoverflow.com/questions/120797/how-do-i-set-the-proxy-to-be-used-by-the-jvm – David Brossard Sep 24 '19 at 19:30
  • @David Brossard Not a duplicate. This question specifically refers to using Microsoft Graph code, not Java in general. – ssimm Sep 25 '19 at 18:41

1 Answers1

1

It looks like you might be missing some configuration while connecting through proxy. Refer to below link for exhaustive details while connecting through proxy.

Connect through a Proxy using Java

You can also try using default network settings to connect through through proxy instead of specifying explicitly using

System.setProperty("java.net.useSystemProxies", "true");
Shubham Kadlag
  • 2,248
  • 1
  • 13
  • 32