Migrate from deprecated MS Graph Android SDK to MS Graph Java SDK

This commit is contained in:
MM20 2022-01-31 13:54:57 +01:00
parent 134d09c3ae
commit bb4a7a1e04
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
5 changed files with 27 additions and 28 deletions

View File

@ -126,12 +126,12 @@ val OpenSourceLicenses = arrayOf(
url = "https://github.com/googleapis/google-api-java-client" url = "https://github.com/googleapis/google-api-java-client"
), ),
OpenSourceLibrary( OpenSourceLibrary(
name = "Microsoft Graph Android SDK", name = "Microsoft Graph SDK for Java",
description = "Deprecated API client for Microsoft Graph APIs", description = "Get started with the Microsoft Graph SDK for Java by integrating the Microsoft Graph API into your Java application!",
licenseName = R.string.mit_license_name, licenseName = R.string.mit_license_name,
licenseText = R.raw.license_mit, licenseText = R.raw.license_mit,
copyrightNote = "Copyright (c) 2015 Microsoft Corporation", copyrightNote = "Copyright (c) 2018 Microsoft Graph",
url = "https://github.com/microsoftgraph/msgraph-sdk-android" url = "https://github.com/microsoftgraph/msgraph-sdk-java"
), ),
OpenSourceLibrary( OpenSourceLibrary(
name = "Microsoft Authentication Library (MSAL) for Android", name = "Microsoft Authentication Library (MSAL) for Android",

View File

@ -27,7 +27,7 @@ allprojects {
} }
} }
maven(url = "https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1") maven(url = "https://pkgs.dev.azure.com/MicrosoftDeviceSDK/DuoSDK-Public/_packaging/Duo-SDK-Feed/maven/v1")
jcenter() // For MS SDK jcenter() // For draglinearlayout
} }
} }

View File

@ -1,6 +1,6 @@
package de.mm20.launcher2.msservices package de.mm20.launcher2.msservices
import com.microsoft.graph.extensions.DriveItem as MSDriveItem import com.microsoft.graph.models.DriveItem as MSDriveItem
data class DriveItem( data class DriveItem(
val id : String, val id : String,

View File

@ -4,11 +4,13 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.util.Log import android.util.Log
import androidx.core.content.edit import androidx.core.content.edit
import com.azure.core.credential.AccessToken
import com.azure.core.credential.TokenCredential
import com.microsoft.graph.authentication.TokenCredentialAuthProvider
import com.microsoft.graph.core.ClientException import com.microsoft.graph.core.ClientException
import com.microsoft.graph.core.DefaultClientConfig
import com.microsoft.graph.extensions.GraphServiceClient
import com.microsoft.graph.extensions.IGraphServiceClient
import com.microsoft.graph.http.GraphServiceException import com.microsoft.graph.http.GraphServiceException
import com.microsoft.graph.models.DriveSearchParameterSet
import com.microsoft.graph.requests.GraphServiceClient
import com.microsoft.identity.client.AuthenticationCallback import com.microsoft.identity.client.AuthenticationCallback
import com.microsoft.identity.client.IAuthenticationResult import com.microsoft.identity.client.IAuthenticationResult
import com.microsoft.identity.client.ISingleAccountPublicClientApplication import com.microsoft.identity.client.ISingleAccountPublicClientApplication
@ -18,6 +20,8 @@ import com.microsoft.identity.client.exception.MsalException
import de.mm20.launcher2.crashreporter.CrashReporter import de.mm20.launcher2.crashreporter.CrashReporter
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import okhttp3.Request
import reactor.core.publisher.Mono
import java.net.URLEncoder import java.net.URLEncoder
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
@ -25,17 +29,14 @@ import kotlin.coroutines.suspendCoroutine
class MicrosoftGraphApiHelper(val context: Context) { class MicrosoftGraphApiHelper(val context: Context) {
private var accessToken: String? = null private var accessToken: String? = null
private val client: IGraphServiceClient private val client: GraphServiceClient<Request> = GraphServiceClient
private var clientApplication: ISingleAccountPublicClientApplication? = null .builder()
.authenticationProvider(TokenCredentialAuthProvider {
Mono.just(AccessToken(accessToken, null))
})
.buildClient()
init { private var clientApplication: ISingleAccountPublicClientApplication? = null
client = GraphServiceClient
.Builder()
.fromConfig(DefaultClientConfig.createWithAuthenticationProvider {
it.addHeader("Authorization", "Bearer $accessToken")
})
.buildClient()
}
private suspend fun getClientApplication(): ISingleAccountPublicClientApplication? { private suspend fun getClientApplication(): ISingleAccountPublicClientApplication? {
val resId = getConfigResId() val resId = getConfigResId()
@ -124,7 +125,7 @@ class MicrosoftGraphApiHelper(val context: Context) {
if (!acquireAccessToken()) return null if (!acquireAccessToken()) return null
return withContext(Dispatchers.IO) { return withContext(Dispatchers.IO) {
try { try {
val user = client.me.buildRequest().get() ?: return@withContext null val user = client.me().buildRequest().get() ?: return@withContext null
val name = user.displayName ?: user.mail ?: "Microsoft User" val name = user.displayName ?: user.mail ?: "Microsoft User"
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit { context.getSharedPreferences(PREFS, Context.MODE_PRIVATE).edit {
putString(PREF_ACCOUNT_NAME, name) putString(PREF_ACCOUNT_NAME, name)
@ -151,8 +152,10 @@ class MicrosoftGraphApiHelper(val context: Context) {
if (!acquireAccessToken()) return null if (!acquireAccessToken()) return null
return try { return try {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
client.me.drive.getSearch( client.me().drive().search(
URLEncoder.encode(query.replace("'", "''"), "utf8") DriveSearchParameterSet.newBuilder()
.withQ(query)
.build()
) )
.buildRequest() .buildRequest()
.select("id,name,file,size,video,image,webUrl,shared,createdBy") .select("id,name,file,size,video,image,webUrl,shared,createdBy")

View File

@ -362,16 +362,12 @@ dependencyResolutionManagement {
.version("30.1.1-android") .version("30.1.1-android")
alias("microsoft.graph") alias("microsoft.graph")
.to("com.microsoft.graph", "msgraph-sdk-android") .to("com.microsoft.graph", "microsoft-graph")
.version("1.7.0") .version("5.13.0")
alias("microsoft.identity") alias("microsoft.identity")
.to("com.microsoft.identity.client", "msal") .to("com.microsoft.identity.client", "msal")
.version("2.2.0") .version("2.2.0")
alias("android.volley")
.to("com.android.volley", "volley")
.version("1.1.1")
version("protobuf", "3.14.0") version("protobuf", "3.14.0")
alias("protobuf.protoc") alias("protobuf.protoc")
.to("com.google.protobuf", "protoc") .to("com.google.protobuf", "protoc")