簡體   English   中英

如何從代理服務器后面的Java應用程序訪問Microsoft Graph Java API?

[英]How to access Microsoft Graph Java API from Java application behind a proxy server?

我正在使用MS圖形Java sdk,以便從我的Java應用程序訪問O365 ODFB驅動器。 當我繞過企業代理服務器(連接到開放式互聯網)時,便能夠從API中獲得所需的響應。 但是當我通過代理服務器連接時,出現以下異常:

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)

我用於連接Graph API的代碼如下:

// 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);

在此行出現錯誤:

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

我可以通過將代理配置傳遞給Authenticator類中的HttpURLConnection.openConnection(proxy)方法來獲取承載令牌。

例如:

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);

我錯過了什么嗎? 請幫忙。

通過代理連接時,您似乎可能缺少某些配置。 通過代理連接時,請參考以下鏈接以獲取詳盡的詳細信息。

使用Java通過代理連接

您也可以嘗試使用默認網絡設置通過代理進行連接,而不是使用

System.setProperty("java.net.useSystemProxies", "true");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM