I have been exploring with Azure and Graph API. I have created two apps, one is a MVC app and other one is a Azure Function app. When I try graphClient.Users.Request().GetAsync(), I am getting data in MVC App but I am facing error like System.Text.Encoding.Web version=6.0.0.0 with Azure Function. However, in the function app too, I am getting data if I try for a specific user like graphClient.Users[Id].Requests().GetAsync(). Here's my code snippet for initializing and making request.
clientId = Environment.GetEnvironmentVariable("ClientId");
tenantId = Environment.GetEnvironmentVariable("TenantId");
clientSecret = Environment.GetEnvironmentVariable("ClientSecret");
_clientApplication = ConfidentialClientApplicationBuilder.Create(clientId)
.WithTenantId(tenantId)
.WithClientSecret(clientSecret)
.Build();
graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) => {
var authResult = await _clientApplication
.AcquireTokenForClient(scopes)
.ExecuteAsync();
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", authResult.AccessToken);
})
var result = await graphClient.Users.Request().Top(100).GetAsync();
My .NET Version is 3.1 and I am using latest Graph API i.e. 4.5.0 and Version of Microsoft.NET.sdk.Functions is 3.1.1. I tried using Filter so as to avoid any internals that might be resulting in my issue but this is of no use. I tried my testcase in other laptop with .net6 and it is working as expected. However, I need to run this with netcore3.1. Please help me resolve this.
In your code snippet, I saw you both used Graph SDK and generating an access token. In my humble opinion, since this is an Azure function, you should add
User.Read.Allapplication API permission then using client credential flow. Could you pls try my code below?I created a function with portal experience and code like below:
I created a file
function.projin kudo which add the reference. Here the runtime must benetstandard2.0.