Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save crpietschmann/d733a57f06fab27d962608341bdef09c to your computer and use it in GitHub Desktop.
Save crpietschmann/d733a57f06fab27d962608341bdef09c to your computer and use it in GitHub Desktop.
Authenticate and Call the Azure AD Graph REST API from Java - Service to Service Authentication
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
// https://hc.apache.org/
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
// https://www.java2s.com/Code/JarDownload/java/java-json.jar.zip
import org.json.*;
public class HelloAzureAD {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, Java!");
HttpClient httpclient = HttpClients.createDefault();
try
{
// OAuth2 is required to access this API. For more information visit:
// https://msdn.microsoft.com/en-us/office/office365/howto/common-app-authentication-tasks
String azureADTenant = "00000000-dd34-4e90-9854-000000000000";
String appId = "00000000-6486-476b-95d5-000000000000";
String appKey = "kddjrhni49dkdne+efdRi+QyKPKYOCScpeObQyHngRgc=";
String appIdUri = "https://graph.windows.net";
System.out.println("Getting access_token token from Azure AD...");
URIBuilder authTokenBuilder = new URIBuilder("https://login.microsoftonline.com/" + azureADTenant + "/oauth2/token");
URI authTokenUri = authTokenBuilder.build();
HttpPost authTokenRequest = new HttpPost(authTokenUri);
List <NameValuePair> authTokenParamsArray = new ArrayList <NameValuePair>();
authTokenParamsArray.add(new BasicNameValuePair("grant_type", "client_credentials"));
authTokenParamsArray.add(new BasicNameValuePair("client_id", appId));
authTokenParamsArray.add(new BasicNameValuePair("client_secret", appKey));
authTokenParamsArray.add(new BasicNameValuePair("resource", appIdUri));
authTokenRequest.setEntity(new UrlEncodedFormEntity(authTokenParamsArray));
HttpResponse authTokenResponse = httpclient.execute(authTokenRequest);
HttpEntity authTokenEntity = authTokenResponse.getEntity();
if (authTokenEntity == null) {
throw new Exception("authTokenEntity is null");
}
String authTokenJsonRaw = EntityUtils.toString(authTokenEntity);
System.out.println("Raw JSON: " + authTokenJsonRaw);
JSONObject authTokenJson = new JSONObject(authTokenJsonRaw);
System.out.println("token_type: " + authTokenJson.getString("token_type"));
System.out.println("expires_in: " + authTokenJson.getString("expires_in"));
String access_token = authTokenJson.getString("access_token");
System.out.println("access_token: " + access_token);
System.out.println("");
System.out.println("Calling Azure AD Graph API...");
// Specify values for path parameters (shown as {...})
URIBuilder builder = new URIBuilder("https://graph.windows.net/" + azureADTenant + "/users");
// You can also look at the permission scopes set for this app by using this API endpoint:
// https://graph.windows.net/{tenant_id}/oauth2PermissionGrants
// This can help to troubleshoot any permissions errors to double-check what permissions are configured.
// Specify values for the following required parameters
builder.setParameter("api-version", "1.6");
URI uri = builder.build();
HttpGet request = new HttpGet(uri);
request.addHeader("Authorization", "Bearer " + access_token);
HttpResponse response = httpclient.execute(request);
System.out.println("HTTP Status Code: " + response.getStatusLine());
HttpEntity entity = response.getEntity();
if (entity != null) {
System.out.println(EntityUtils.toString(entity));
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment