无法从共享点调用 Microsoft Graph api

Can't call microsoft graph api from sharepoint

我创建了一个 Sharepoint webpart 2019,并想连接到 Microsoft Graph api。但是,在调用 api 时出现以下错误:

"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."

这是我的代码:

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["user@example.com"].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;
}

用户界面简单

<asp:TextBox ID="test" runat="server"></asp:TextBox>

我注意到我的 nuget 包是蓝色的。不确定这是否是我收到错误的原因

当我 运行

时出现错误
var user = await Client.Users["user@example.com"].Request().GetAsync();

您的 .Net FrameWork 是什么版本? System.Text.Json NuGet 包仅支持 .NET Framework 4.7.2 及更高版本。

您可以在此处查看文档:https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-overview?pivots=dotnet-5-0