Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
import com.azure.identity.UsernamePasswordCredential;
import com.azure.identity.UsernamePasswordCredentialBuilder;
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
import com.microsoft.graph.requests.GraphServiceClient;
import com.microsoft.graph.requests.UserCollectionPage;
import java.util.Arrays;
public class TestConnectionAzureAD {
    public static void main(String[] args) {
        UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()
                .clientId("Client_Id")
                .username("User_Name")
                .password("Password")
                .build();
        final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(Arrays.asList("https://graph.microsoft.com/.default"), usernamePasswordCredential);
        final GraphServiceClient graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(tokenCredentialAuthProvider)
                .buildClient();
        UserCollectionPage userCollectionpage = graphClient.users().buildRequest().get();
        System.out.println("--");

I am using - kotlin-stdlib-1.6.20.jar, microsoft-graph-5.31.0.jar, microsoft-graph-core-2.0.13.jar, okhttp-4.0.1.jar -- these jars...

I am getting this error -

Exception in thread "main" java.lang.NoSuchFieldError: Companion
    at okhttp3.internal.Util.<clinit>(Util.kt:69)
    at okhttp3.internal.connection.RealConnectionPool.<clinit>(RealConnectionPool.kt:261)
    at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:37)
    at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:39)
    at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.kt:430)
    at com.microsoft.graph.httpcore.HttpClients.custom(HttpClients.java:28)
    at com.microsoft.graph.httpcore.HttpClients.createDefault(HttpClients.java:44)
    at com.microsoft.graph.core.BaseClient$Builder.getHttpClient(BaseClient.java:180)
    at com.microsoft.graph.core.BaseClient$Builder.getHttpProvider(BaseClient.java:188)
    at com.microsoft.graph.core.BaseClient$Builder.buildClient(BaseClient.java:276)
    at com.microsoft.graph.requests.GraphServiceClient$Builder.buildClient(GraphServiceClient.java:258)

solution ?

My guess is that you are having problems with dependencies and incompatible versions. Fiddle around with your classpath for libraries and/or try other/older versions of okhttp. – funky Aug 2, 2022 at 8:42 Thanks for your suggestions. I was using "okhttp-4.0.1.jar" then I have used this - "okhttp-4.0.1.jar", but it didn't work... But after your suggestion when I use the older version ( "okhttp-3.11.0.jar" ), it works properly... – Subhadip Dutta Aug 2, 2022 at 9:32 The issue was resolved for me after adding "com.squareup.okhttp3" version 4.10.0 dependency in pom.xml – Indrapal Singh Nov 17, 2022 at 5:03

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.