Reputation: 1934
I create a sharepoint webpart 2019, and want to connect to Microsoft Graph api. However when calling the api i get the following error:
"Could not load file or assembly 'System.Text.Json, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified."
This is my code:
protected void Page_Load(object sender, EventArgs e)
{
connectGraph().GetAwaiter().GetResult();
}
private async Task<GraphServiceClient> connectGraph()
{
try
{
string Tenant = "*************************************";
string ClientId = "*************************************";
string ClientSecret = "*************************************";
GraphServiceClient Client;
PublicClientApplicationBuilder clientApp = PublicClientApplicationBuilder.Create(ClientId);
IAuthenticationProvider authenticationProvider = new DelegateAuthenticationProvider(async (requestMessage) =>
{
if (BearerToken == null || BearerToken.IsExpired())
{
string accessToken = GetUserAccessTokenAsync(clientApp.Build());
}
// Append the access token to the request.
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", BearerToken.access_token);});
Client = new GraphServiceClient(authenticationProvider);
var user = await Client.Users["[email protected]"].Request().GetAsync();
test.Text = user.DisplayName;
}
}
}
private string GetUserAccessTokenAsync(IPublicClientApplication clientApp)
{
string[] Scopes = { "Directory.AccessAsUser.All" };
string token = null;
AuthenticationResult authResult = clientApp.AcquireTokenInteractive(Scopes).ExecuteAsync().Result;
token = authResult.AccessToken;
BearerToken = new Token();
BearerToken.access_token = token;
return token;
}
public class Token
{
public string access_token;
public int expires_in;
public int expires_on;
public int not_before;
public string token_type;
}
The user interface is simple
<asp:TextBox ID="test" runat="server"></asp:TextBox>
I notice my nuget package are blue. Not sure if this is the cause of why I get the error
The errors occurs when i run the
var user = await Client.Users["[email protected]"].Request().GetAsync();
Upvotes: 1
Views: 183
Reputation: 3655
What's the version of your .Net FrameWork? The System.Text.Json NuGet package only supports .NET Framework 4.7.2 and later versions.
You could see the documentation here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-overview?pivots=dotnet-5-0
Upvotes: 1