The Active Directory Authentication Library for Java (ADAL4J) allows authentication via access token to the Microsoft Graph API, using the following (simplified) code:
public String authenticate(String authorizationUrl, String clientId, String clientSecret) throws Exception {
ExecutorService service = Executors.newFixedThreadPool(1);
AuthenticationContext context = new AuthenticationContext(authorizationUrl, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = context.acquireToken(“https://graph.microsoft.com”, credential, null);
return future.get().getAccessToken();
}
The above works for certain parts of Graph (e.g., for accessing Office 365
accounts), but does not work for OneDrive
, where it returns an access token that does not have proper authorization.
Acquiring an access token via POSTMAN works as expected, with the following parameters:
authorizationUrl: https://login.microsoftonline.com/common/oauth2/v2.0/authorize
accessTokenUrl: https://login.microsoftonline.com/common/oauth2/v2.0/token
clientId: <the clientId for the application>
clientSecret: <the clientSecret for the application>
scope: https://graph.microsoft.com/.default
state: <empty>
More specifically, running the above in POSTMAN returns an access token with additional scopes, including https://graph.microsoft.com/Files.ReadWrite.All
. Using that access token in the Java
application that calls the authenticate()
method above, does work, e.g. it lists the contents of the root directory using /me/drive/root/children
as the REST path.
If, however, the access token returned by the authenticate()
method is used, an error is returned by OneDrive
. Removing the user name (me
) from the path returns only 1 file name, if the specific tenant ID is used instead of common
in the authorizationUrl.
There seems to be no way to add a scope value in ADAL4J
and numerous other variations either result in an error, or in getting back 1 file (probably from a different context).
Is there any way to get a fully authorized access token via ADAL4J, for OneDrive?