Microsoft Graph API , using ADAL4J

1.2k views Asked by At

I'm trying to create a simple app that allows me to list and edit user accounts on Azure Active Directory.

These users were added to the directory through Azure AD B2C.

I followed some samples and after a few hours of research i've come up with a simple piece of code that attempts to obtain a token from Azure AD and with that token tries to query the Graph API to obtain a list of users.

The samples that i've take a look were:

https://github.com/AzureAD/azure-activedirectory-library-for-java

https://github.com/Azure-Samples/active-directory-java-native-headless

And the Library WIKI

https://github.com/AzureAD/azure-activedirectory-library-for-java/wiki

But for some reason i get an 401 Unauthenticated Status code from azure, with the following error message:

{'odata.error':{'code':'Authentication_MissingOrMalformed','message':{'lang':'en','value':'Access Token missing or malformed.'},'date':'2018-08-02T18:39:48','requestId':'7ddb7d55-2074-4124-9863-a19626a6b49f','values':null}}

even when sending the authorization header.

One thing is that for authenticating the APP in Azure AD i'm using an APP registered in Azure AD App Registrations.

I've compiled a sample project with all the requirements, available on github

https://github.com/pedrorochaorg/Microsoft-ADAL-Sample

Can someone please show me the right way to achieve this?

1

There are 1 answers

0
Tom Sun On

According to the error message Authentication_MissingOrMalformed. The access token you get is not for the corresponding resource.

Based on you mentioned code , the resource url should be https://graph.windows.net not the APP_ID_URL

 String resourceUrl = "https://graph.windows.net";
 Future<AuthenticationResult> result = context.acquireToken(
                resourceUrl,    
                new ClientCredential(APP_ID, APP_SECRET),
                null
        );

And you are using the Azure AD graph API. You just need to set the permission for Azure Windows Azure Active Directory and don't forgot to grant the permission.

A space and a header Accept:application/json is needed for following code.

conn.setRequestProperty("Accept","application/json");
conn.setRequestProperty("Authorization", result.get().getAccessTokenType() + " "+ result.get().getAccessToken());

Test result:

enter image description here