2

I am trying to implement GraphClient, below is a sample code which is working fine...

   ClientCredentialProvider authProvider = 
              new ClientCredentialProvider(clientId,
                      scopes,
                      clientSecret,
                      b2cTenant,
                      endpoint);
    
    IGraphServiceClient graphClient = graphClient = GraphServiceClient.builder()
                .authenticationProvider(authProvider)
                .buildClient();

This is working fine...but in some case from where the code is being run, there is a proxy, so I need to setup proxy to connect to internet. I need to setup proxy and pass it to graphClient somehow to tell make a call via proxy.

I was trying to find a document but could not get any through I got this...

ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress(proxyUrl, proxyPort));
proxyOptions.setCredentials(proxyUser, proxyPassword);

final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()
                    .clientId(clientId)
                    .username(username)
                    .password(password)
                    .httpClient(HttpClient.createDefault(new HttpClientOptions().setProxyOptions(proxyOptions)))
                    .build();

But the problem is the "ProxyOptions" isnt in Maven and I am not sure what library it is part of.

Can anyone suggest an idea.

2 Answers 2

6

Updating the answer...

        ProxyOptions proxyOptions = new ProxyOptions(
               ProxyOptions.Type.HTTP, new InetSocketAddress(hostAddress, hostPort));
        
        HttpClientOptions clientOptions = new HttpClientOptions();
        clientOptions.setProxyOptions(proxyOptions); 
        
        HttpClient azHttpClient = HttpClient.createDefault(clientOptions);
        
        ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
                .clientId(clientId)
                .clientSecret(clientSecret)
                .tenantId(tenantId)
                .httpClient(azHttpClient)
                .build();

        TokenCredentialAuthProvider tokenCredentialAuthProvider = 
            new TokenCredentialAuthProvider(scopes, clientSecretCredential);
        
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(hostAddress, hostPort));
        
        OkHttpClient httpClient = HttpClients.createDefault(tokenCredentialAuthProvider)
                .newBuilder()
                .proxy(proxy)
                .build();

        graphClient = GraphServiceClient.builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .httpClient(httpClient)
                .buildClient();
Sign up to request clarification or add additional context in comments.

8 Comments

in terms of Maven, <dependency> <groupId>com.azure</groupId> <artifactId>azure-core-http-netty</artifactId> <version>1.9.2</version> </dependency>
Please - where is the DefaultClientConfig defined? cannot find it in any dependency :(
@Frk I am updating the answer, MS has updated the library.
@Frk Proxy class belongs to java.net.Proxy and OkHttpClient belongs to okhttp3 library, everything else is from com.azure.
Please upvote if this solves your issue, thank you!
|
0

If you can use system properties then try to configure via

https.proxyHost and https.proxyPort

1 Comment

Never change system properties like this. NOT recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.