How to use Graph API

Vinay Raj
1 min readJul 25, 2023

--

Using the Graph API in Java typically involves making HTTP requests to the Microsoft Graph API endpoints.

To get started, you’ll need to set up an Azure AD application, obtain the required access tokens, and then make HTTP requests.

Prerequisites

1. Set up Azure AD Application

2. Generate Access Token

3. Make HTTP Requests

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.json.JSONArray;
import org.json.JSONObject;

public class GraphApiExample {

public static void main(String[] args) {
// Replace these with your application's values
String accessToken = "YOUR_ACCESS_TOKEN";
String graphEndpoint = "https://graph.microsoft.com/v1.0/me/drive/root/children";

HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(graphEndpoint))
.header("Authorization", "Bearer " + accessToken)
.build();

try {
HttpResponse<InputStream> response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
String jsonResponse = new String(response.body().readAllBytes());

} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
}
}

NOTE: This is an example how graph API can be used in java. For details of APIs Kindly follow Microsoft Graph documentation… Or wait for more contents from me :)

--

--