Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use v6 with updated msal authentication library for Android #2137

Open
gerritbeuze opened this issue Aug 27, 2024 · 2 comments
Open
Labels
type:documentation General documentation request or project documentation update

Comments

@gerritbeuze
Copy link

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 gerritbeuze added the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Aug 27, 2024
@gerritbeuze gerritbeuze changed the title How to use v6 with How to use v6 with updated msal authentication library for Android Sep 2, 2024
@Ndiritu Ndiritu added type:documentation General documentation request or project documentation update and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned labels Sep 2, 2024
@diyoyo
Copy link

diyoyo commented Sep 16, 2024

Same here, any hint?

@diyoyo
Copy link

diyoyo commented Sep 19, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:documentation General documentation request or project documentation update
Projects
None yet
Development

No branches or pull requests

3 participants