Trying to call graph calendar API using ClientSecret flow from java code but I am getting url not found error.The same works from postman.

Ravindran, Rohit 1 Reputation point
2021-12-30T06:52:23.987+00:00

Trying to call graph calendar API using ClientSecret flow from java code but I am getting url not found error from code.

Below is the code being used - the same ClientSecret flow works for getting the AD details of user.

public class ClientSecretFlow {

private final static List<String> SCOPES = Arrays.asList("https://graph.microsoft.com/.default");

public static void main(String[] args) throws Exception {

    final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
            .clientId("81df77....").clientSecret("BHf7Q.....")
            .tenantId("c4714741....").build();

    final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(SCOPES,
            clientSecretCredential);


    final OkHttpClient httpClient = HttpClients.createDefault(tokenCredentialAuthProvider);


    String json = "{\"subject\": \"Event testing\",\"start\": {\"dateTime\": \"2021-12-15T08:34:45.317Z\",\"timeZone\": \"UTC\"},\"end\": { \"dateTime\": \"2021-12-20T08:34:45.317Z\", \"timeZone\": \"UTC\"}}";

    RequestBody formBody = RequestBody.create(MediaType.parse("application/json"), json);


    Request calendarrequest = new Request.Builder()
            .url("https://graph.microsoft.com/v1.0/users/{userid}/calendar/events").post(formBody)
            .build();

    try {
        Response response = httpClient.newCall(calendarrequest).execute();

        System.out.println(response);

        // Do something with the response.
    } catch (IOException e) {
        e.printStackTrace();
    }
    // sending outlook mails for calendar invite

}

}

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,371 Reputation points
    2022-01-07T09:26:18.32+00:00

    Hi @Ravindran, Rohit

    This error is usually because your user does not have an MS 365 license, make sure your user has been granted an MS 365 license before calling the /calendar/events endpoint.

    163122-image.png


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


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.