Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to GET application by appId using Microsoft Graph API via Java using HttpURLConnection

I want to authorize an OAuth JSON Web Token granted by Azure Active Directory, and one thing I need to do is get more information about the application in the token's appId using the Microsoft Graph API.

Microsoft Graph API allows me to GET an app by its id via

https://graph.microsoft.com/beta/applications/{id}

, but NOT by its appId via

https://graph.microsoft.com/beta/applications/{appId}

The best way I see to use Microsoft Graph API to GET an app using its AppId is via a filter like so:

https://graph.microsoft.com/beta/applications?filter=appId eq '{appId}'

The above filter works just fine in the Microsoft Graph Explorer, but when calling the Graph API using a GET request using HttpUrlConnection, my request fails with HTTP Code 400 and message "Bad Request".

It's weird because using the exact same HttpUrlConnection to GET the full range of applications via

https://graph.microsoft.com/beta/applications

works just fine.

Is there something about the filter functionality that I can't use it in a Microsoft Graph API GET request? How should I get info on an app by its AppId?

Here is a snippet of the Java code I am using for my HttpURLConnection:

url = new URL(String.format("https://graph.microsoft.com/beta/applications?filter=appId eq '%s'", appId));
        final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Bearer " + result.getAccessToken());
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Content-Type", "application/json");

        final int httpResponseCode = conn.getResponseCode();
        if (httpResponseCode == 200 || httpResponseCode == 201) {
            BufferedReader in = null;
            final StringBuilder response;
            try {
                in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()));
                String inputLine;
                response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
            } finally {
                in.close();
            }
            final JSONObject json = new JSONObject(response.toString());
            return json.toString(4);
        } else {
            return String.format("Connection returned HTTP code: %s with message: %s",
                    httpResponseCode, conn.getResponseMessage());
        }
like image 488
Sammy Cakes Avatar asked Sep 22 '26 07:09

Sammy Cakes


2 Answers

In case someone else comes looking for this, if you are using the GraphServiceClient you can do this:

var appId = "some app id";

var response = await _graphClient.Applications
    .Request()
    .Filter($"appId eq '{appId}'")
    .GetAsync();

var azureAddApplication = response.FirstOrDefault() ?? throw new ArgumentException($"Couldn't find App registration with app id {appId}");
like image 51
dglozano Avatar answered Sep 23 '26 23:09

dglozano


In case someone is searching this... the API call should be done with the app's Object ID, not the app ID.

like image 44
Hannah Zuber Avatar answered Sep 23 '26 23:09

Hannah Zuber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!