msgraph-sdk-java icon indicating copy to clipboard operation
msgraph-sdk-java copied to clipboard

How to use v6 with updated msal authentication library for Android

Open gerritbeuze opened this issue 1 year ago • 4 comments

The Microsoft Graph sample Android app https://github.com/microsoftgraph/msgraph-sample-android no longer compiles with msgraph-sdk-java version 6.

There is an updated msal authentication library for Android https://github.com/AzureAD/microsoft-authentication-library-for-android and that works: I can sign in and out in a single account client application. But I have no idea how to connect to this library Microsoft Graph SDK for Java

Particularly: the old project had a class AuthorizationHelper that bridged for getting updated tokens between the signed in ClientApplication and the GraphServiceClient.

Any example or documentation on how build that connection with this v6 library?

gerritbeuze avatar Aug 27 '24 13:08 gerritbeuze

Same here, any hint?

diyoyo avatar Sep 16 '24 08:09 diyoyo

Preliminary comment

Replying to last message on #2160 and to the question at hand:

  • I ended up creating my own APIHelper class with the help of ChatGPT, doing only the few calls I need to make. At the end of the day, I decided not to use the MSGraph-SDK, since each step forward brings 1000 questions from all the outdated documentation out there.

  • Anyways, if you really want to do it, Gemini suggested what looks like a simple and not so far-fetched workaround. I haven't tested it yet, but I like the idea so far:

Question asked to Gemini within Android Studio:

Please explain from the code of this project how can I instantiate a GraphServiceClient when all I have is an AccessToken string?

Reply

You can instantiate a GraphServiceClient with an authentication token using the GraphServiceClient.builder() method. First, create an OkHttpClient and add an Authenticator to it. The authenticator will be responsible for adding the authentication token to the request header.

val httpClient = OkHttpClient.Builder()
    .authenticator(object : Authenticator {
        override fun authenticate(route: Route?, response: Response): Request? {
            val requestBuilder = response.request.newBuilder()
            requestBuilder.header("Authorization", "Bearer $authenticationToken")
            return requestBuilder.build()
        }
    })
    .build()

Then, use the GraphServiceClient.builder() to create a new GraphServiceClient instance with the OkHttpClient you just created.

val graphServiceClient = GraphServiceClient.builder()
    .httpClient(httpClient)
    .buildClient()

This will create a GraphServiceClient that will use the provided authentication token for all requests.

diyoyo avatar Sep 19 '24 11:09 diyoyo

Unfortunately, as of the current version (6.55.0), GraphServiceClient.builder() does not exist.

val graphServiceClient = GraphServiceClient.builder()
    .httpClient(httpClient)
    .buildClient()

This will create a GraphServiceClient that will use the provided authentication token for all requests.

ryank262 avatar Nov 04 '25 20:11 ryank262

For anyone who stumbles upon this, I was able to get the Graph SDK working using slowcar's solution posted in 2180

However, I excluded the OkHttp client in the arguments of the GraphServiceClient constructor call. So it looked like this:

BaseBearerTokenAuthenticationProvider...

GraphServiceClient(authProvider)

ryank262 avatar Nov 04 '25 21:11 ryank262