Skip to content

Commit

Permalink
update functions to use Graph API 6.X
Browse files Browse the repository at this point in the history
  • Loading branch information
dkontyko committed Feb 11, 2024
1 parent d732baf commit 4d2b7e0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion FunctionApp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>5.80.0</version>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -247,26 +247,23 @@ 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 <T> TokenCredentialAuthProvider getTokenCredAuthProv(final HttpRequestMessage<T> request) {
private static <T> TokenCredential getTokenCredential(final HttpRequestMessage<T> 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"))
.userAssertion(incomingAccessToken)
.additionallyAllowedTenants(Collections.singletonList("*"))
.build();

return new TokenCredentialAuthProvider(scopes, oboCredential);

}
}

Expand All @@ -276,19 +273,21 @@ private static <T> 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<Request> 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();

Expand Down

0 comments on commit 4d2b7e0

Please sign in to comment.