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"
),
OpenSourceLibrary(
name = "Microsoft Graph Android SDK",
description = "Deprecated API client for Microsoft Graph APIs",
name = "Microsoft Graph SDK for Java",
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,
licenseText = R.raw.license_mit,
copyrightNote = "Copyright (c) 2015 Microsoft Corporation",
url = "https://github.com/microsoftgraph/msgraph-sdk-android"
copyrightNote = "Copyright (c) 2018 Microsoft Graph",
url = "https://github.com/microsoftgraph/msgraph-sdk-java"
),
OpenSourceLibrary(
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")
jcenter() // For MS SDK
jcenter() // For draglinearlayout
}
}

View File

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

View File

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

View File

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