From 4d2b7e0305dc2f604d0aa08263bec11880e4652f Mon Sep 17 00:00:00 2001
From: David Kontyko <33853225+dkontyko@users.noreply.github.com>
Date: Sun, 11 Feb 2024 14:46:33 -0500
Subject: [PATCH] update functions to use Graph API 6.X
---
FunctionApp/pom.xml | 2 +-
.../functions/HttpTriggerFunctions.java | 39 +++++++++----------
2 files changed, 20 insertions(+), 21 deletions(-)
diff --git a/FunctionApp/pom.xml b/FunctionApp/pom.xml
index f55592f..44f7067 100644
--- a/FunctionApp/pom.xml
+++ b/FunctionApp/pom.xml
@@ -26,7 +26,7 @@
com.microsoft.graph
microsoft-graph
- 5.80.0
+ 6.1.0
com.azure
diff --git a/FunctionApp/src/main/java/app/djk/RestPdfFormFiller/functions/HttpTriggerFunctions.java b/FunctionApp/src/main/java/app/djk/RestPdfFormFiller/functions/HttpTriggerFunctions.java
index 2d3e337..0a1219a 100644
--- a/FunctionApp/src/main/java/app/djk/RestPdfFormFiller/functions/HttpTriggerFunctions.java
+++ b/FunctionApp/src/main/java/app/djk/RestPdfFormFiller/functions/HttpTriggerFunctions.java
@@ -6,15 +6,15 @@
import app.djk.RestPdfFormFiller.projectExceptions.InvalidReturnDataFormatException;
import app.djk.RestPdfFormFiller.projectExceptions.InvalidSessionIdException;
import app.djk.RestPdfFormFiller.projectExceptions.InvalidXfaFormException;
+import com.azure.core.credential.TokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.identity.OnBehalfOfCredentialBuilder;
import com.microsoft.azure.functions.*;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
-import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
-import com.microsoft.graph.core.ClientException;
-import com.microsoft.graph.requests.GraphServiceClient;
+import com.microsoft.graph.serviceclient.GraphServiceClient;
+import com.microsoft.kiota.ApiException;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@@ -197,7 +197,7 @@ private HttpResponseMessage errorHandler(final HttpRequestMessage> request,
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Invalid session ID.").build();
}
// Dependency and built-in exceptions
- catch (ClientException e) {
+ catch (ApiException e) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Could not retrieve file.").build();
} catch (NumberFormatException e) {
return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Invalid integer argument in request.").build();
@@ -247,17 +247,16 @@ private static String validateSiteID(final String siteID) {
* @return A token credential auth provider generated from either the default Azure credential or
* an on-behalf-of credential.
*/
- private static TokenCredentialAuthProvider getTokenCredAuthProv(final HttpRequestMessage request) {
+ private static TokenCredential getTokenCredential(final HttpRequestMessage request) {
final var authHeader = request.getHeaders().get("authorization");
if (authHeader == null) {
// Right now this is limited to local test cases
- return new TokenCredentialAuthProvider(new DefaultAzureCredentialBuilder().build());
+ return new DefaultAzureCredentialBuilder().build();
} else {
final var incomingAccessToken = authHeader.substring(7); // removing "Bearer" prefix and space
- final var scopes = Collections.singletonList("https://graph.microsoft.com/.default");
- final var oboCredential = new OnBehalfOfCredentialBuilder()
+ return new OnBehalfOfCredentialBuilder()
.tenantId(System.getenv("tenantId"))
.clientId(System.getenv("clientId"))
.clientSecret(System.getenv("clientSecret"))
@@ -265,8 +264,6 @@ private static TokenCredentialAuthProvider getTokenCredAuthProv(final HttpRe
.additionallyAllowedTenants(Collections.singletonList("*"))
.build();
- return new TokenCredentialAuthProvider(scopes, oboCredential);
-
}
}
@@ -276,19 +273,21 @@ private static InputStream getFileInputStreamFromSpo(final HttpRequestMessag
final var listID = UUID.fromString(request.getQueryParameters().getOrDefault("listID", ""));
final var itemID = Integer.parseInt(request.getQueryParameters().getOrDefault("itemID", ""));
- final var tokenCredential = getTokenCredAuthProv(request);
- final GraphServiceClient graphClient = GraphServiceClient
- .builder()
- .authenticationProvider(tokenCredential)
- .buildClient();
+ final var tokenCredential = getTokenCredential(request);
+ final GraphServiceClient graphClient = new GraphServiceClient(
+ tokenCredential,
+ "https://graph.microsoft.com/.default"
+ );
final var result = graphClient
- .sites(siteID)
- .lists(listID.toString())
- .items(Integer.toString(itemID))
+ .sites()
+ .bySiteId(siteID)
+ .lists()
+ .byListId(listID.toString())
+ .items()
+ .byListItemId(Integer.toString(itemID))
.driveItem()
- .content()
- .buildRequest();
+ .content();
final var fileStream = result.get();