【问题标题】:How to access Microsoft Graph Java API from Java application behind a proxy server?如何从代理服务器后面的 Java 应用程序访问 Microsoft Graph Java API?
【发布时间】:2019-04-04 09:14:47
【问题描述】:

我正在使用 MS graph 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);

我错过了什么吗?请帮忙。

【问题讨论】:

  • 您的企业代理是否需要身份验证?这里有一个相关的帖子stackoverflow.com/questions/1432961/…你能提供更多的设置代码来了解这是如何配置的吗?
  • @Darrel 是的,我的代理服务器需要身份验证。我已经用设置代码更新了我的问题。
  • 这个问题有什么解决办法吗?谢谢
  • @David Brossard 不是重复的。这个问题专门指的是使用 Microsoft Graph 代码,而不是一般的 Java。

标签: java office365 microsoft-graph-api microsoft-graph-sdks


【解决方案1】:

看起来您在通过代理连接时可能缺少一些配置。 通过代理连接时,请参阅下面的链接了解详细信息。

Connect through a Proxy using Java

您也可以尝试使用默认网络设置通过代理进行连接,而不是明确指定使用

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

【讨论】:

  • 能否添加一个包含问题中使用的代码的代码示例?
猜你喜欢
  • 1970-01-01
  • 2015-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多