【问题标题】:SocketTimeOutException for PROXY call using GraphServiceClient, how to configure proxy使用 GraphServiceClient 进行 PROXY 调用的 SocketTimeOutException,如何配置代理
【发布时间】:2021-06-04 10:14:42
【问题描述】:

我正在尝试从 Azure B2C Active Directory 获取用户列表,对于非代理环境,我的代码运行良好,但是当我通过传递代理配置运行它时,我收到“SocketTimeoutException”

下面是我的代码...

GraphServiceClient<Request> graphClient;
    
    if (this.proxy.equals("true")) {      
        
        final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId(this.clientId)
                .clientSecret(this.clientSecret)
                .tenantId(this.b2cTenant)
                .build();

        final TokenCredentialAuthProvider tokenCredentialAuthProvider = 
            new TokenCredentialAuthProvider(Constant.scopes, clientSecretCredential);

        final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(this.hostAddress, this.hostPort));
        
        final OkHttpClient httpClient = HttpClients.createDefault(tokenCredentialAuthProvider)
                .newBuilder()
                .proxy(proxy)
                .build();
                
        graphClient = GraphServiceClient.builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .httpClient(httpClient)
                .buildClient();
        
    } else {
        
        final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId(this.clientId)
                .clientSecret(this.clientSecret)
                .tenantId(this.b2cTenant)
                .build();

        final TokenCredentialAuthProvider tokenCredentialAuthProvider = 
            new TokenCredentialAuthProvider(Constant.scopes, clientSecretCredential);
        
        graphClient = GraphServiceClient.builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .buildClient();
    }

在“if”中我使用 PROXY,而在“else”中我在没有 PROXY 的情况下使用。

所以我有一个通过命令行传递的 hostAddress 和 hostPort...我正在使用 clientId、clientSecret 和 b2cTenantId 创建一个 ClientSecretCredential

然后我正在使用范围和客户端机密凭据创建 TokenCredentialAuthProvider

对我来说,范围是 - https://graph.microsoft.com/.default

然后我使用地址和端口创建一个代理,并将其传递给 OkHttpClient。然后我正在创建将所有内容传递给 graphClient。

对于非代理(“else”)工作正常,但是当我通过代理工作时,我得到“超时”。我尝试调试代码,但调用 API 时唯一的例外是......

final User me = graphClient.me().buildRequest().get();

我收到“SocketTimeoutException”

我已经阅读了多个文档,github线程,我无法理解问题。

https://github.com/microsoftgraph/msgraph-sdk-java/issues/162

https://github.com/microsoftgraph/msgraph-sdk-java/issues/158

https://github.com/microsoftgraph/msgraph-sdk-java-core

请帮忙。

【问题讨论】:

    标签: java microsoft-graph-api azure-ad-b2c


    【解决方案1】:

    您的代码错误,/me 端点不支持客户端凭据流。修改你的代码,问题应该就解决了。

    如果要在整个租户中list users,则需要使用/users端点:

    final Users users = graphClient.users().buildRequest().get();

    如果你只需要list a certain user租户的信息,那么你可以使用/users/{user id}端点:

    final User user = graphClient.users("{user id}").buildRequest().get();

    【讨论】:

      【解决方案2】:

      在使用客户端凭据生成器时,您还需要为 Azure 身份客户端配置代理,如下所示

      https://github.com/microsoftgraph/microsoft-graph-docs/pull/12779/commits/89729042a122ecaf4c083ccc18a9e7d565bbec25

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 2022-11-02
        • 1970-01-01
        相关资源
        最近更新 更多