List all Messages in GraphClient API

Anonymous
2023-04-23T17:46:13.18+00:00

com.microsoft.graph.core.ClientException: Error during http request. How to solve this problem

GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider( authProvider ).buildClient();

MessageCollectionPage messages = graphClient.me().messages()
	.buildRequest()
	.select("sender,subject")
	.get();

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,366 Reputation points
    2023-04-24T10:18:42.63+00:00

    Hi @Venkatesh Nambiyar

    You can try hardcoding your username/password, refer to the sample snippet.

            final UsernamePasswordCredential usernamePasswordCredential = new UsernamePasswordCredentialBuilder()
                    .clientId(client id)
                    .username(user name)
                    .password(password)
                    .build();
    
            List<String> scopes = new ArrayList<String>();
    
            scopes.add("https://graph.microsoft.com/.default");
    
            final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(scopes, usernamePasswordCredential);
    
    
            final GraphServiceClient graphClient =
                    GraphServiceClient
                            .builder()
                            .authenticationProvider(tokenCredentialAuthProvider)
                            .buildClient();
    
            final MessageCollectionPage messages = graphClient.me().messages()
                    .buildRequest()
                    .select("sender,subject")
                    .get();
    

    Here is my maven dependency:

        <dependency>
            <!-- Include the sdk as a dependency -->
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>5.54.0</version>
        </dependency>
        <dependency>
            <!-- This dependency is only needed if you are using the TokenCrendentialAuthProvider -->
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.2.5</version>
        </dependency>
    

    Debug my code: User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.