【问题标题】:MS Graph API returns 500 internal server error when querying sharepoint site查询共享点站点时,MS Graph API 返回 500 内部服务器错误
【发布时间】:2021-02-05 13:02:47
【问题描述】:

简短说明:

我尝试从我的测试共享点查询站点:{my_name}.sharepoint.com 使用来自 http 请求的本机 REST 接口以及使用 Graph SDK。身份验证很好,我可以使用这两种方法获取令牌。我已经在 portal.azure.com 上进行了应用注册、授予权限并为他们提供了管理员同意。

身份验证:

http请求代码:

                FormUrlEncodedContent content = new FormUrlEncodedContent(new[] {                 
                    new KeyValuePair<string, string>("client_id", $"{ClientId}"),
                    new KeyValuePair<string, string>("scope", "https://graph.microsoft.com/.default"),
                    new KeyValuePair<string, string>("grant_type", "client_credentials"),
                    new KeyValuePair<string, string>("client_secret", ClientSecret)
                });
                string url = $"https://login.microsoftonline.com/{TenantId}/oauth2/v2.0/token";
                Console.WriteLine(url);
                var message = new HttpRequestMessage(HttpMethod.Post, url);
                message.Content = content;
                message.Content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
                message.Headers.Accept.Clear();
                message.Headers.Accept.TryParseAdd("application/json");
                var response = await httpClient.SendAsync(message);

 

图形 SDK 代码:

            IConfidentialClientApplication app = 
                ConfidentialClientApplicationBuilder
                    .Create(clientId)
                    .WithClientSecret(clientSecret)
                    .WithAuthority(new Uri($"https://login.microsoftonline.com/{GetWWWAuthResponseHeaders(domain)["Bearer realm"]}")
                ).Build();
                var authenticationResult = await app.AcquireTokenForClient(new string[] { "https://graph.microsoft.com/.default" }).ExecuteAsync();

 

            var graphClient = new GraphServiceClient(
                new DelegateAuthenticationProvider(requestMessage => {
                    requestMessage.Headers.Authorization =
                        new AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);
                    return Task.FromResult(0);
                })
            );

产生错误的请求:

原生 http 代码:

                string url = $"https://graph.microsoft.com/v1.0/sites/{Domain}:/sites/{Site}";
                Console.WriteLine($"url: {url}");
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
                request.Headers.Add("Authorization", $"Bearer {Token.Token}");
                request.Headers.Add("Accept", "application/json");
                HttpResponseMessage response = await httpClient.SendAsync(request);
                responseBody = await response.Content.ReadAsStringAsync();

图形 SDK 代码:

var site = graphClient.Sites[$"{my_name}.sharepoint.com"].Request().GetAsync().Result;

我得到的确切错误:

ServiceException: Code: generalException
Message: An unspecified error has occurred.
Inner error:
    AdditionalData:
    date: 2021-02-05T10:02:19
    request-id: a3567eca-3d3b-4617-b877-e8f7369660b3
    client-request-id: a3567eca-3d3b-4617-b877-e8f7369660b3
ClientRequestId: a3567eca-3d3b-4617-b877-e8f7369660b3

【问题讨论】:

  • Baliant,在您的应用程序之外尝试使用具有相同 Graph API 调用的 POSTMAN/Graph Explorer 重现该问题,看看您是否仍然可以重现该问题。我试过了,但我没能在最后重现这个问题。
  • 当您使用客户端凭据授予时,获取该令牌并使用 curl 或 POSTMAN 调用此 Graph API https://graph.microsoft.com/v1.0/sites/domain.sharepoint.com 并查看结果。
  • 我也从 Postman 收到同样的 500 Internal Server 错误。根据集合,我可以从login.microsoftonline.com{{TenantID}}/oauth2/v2.0/token 获取令牌,我正在调用graph.microsoft.com/v1.0/sites{{Domain}}.sharepoint.com 并获得500。我在 jwt.io 上检查了我的令牌,我觉得它看起来不错,如果我故意弄乱令牌,我会得到 401,如你所料。

标签: sharepoint microsoft-graph-api microsoft-graph-sdks


【解决方案1】:

我用这段代码测试过,效果很好。

            string clientID = "cde921c5-cccc-4264-a450-6daceb46fec5"; // Put the Application ID from above here.
            string clientSecret = "clientSecret "; // Put the Client Secret from above here.

            string graphApiResource = "https://graph.microsoft.com";
            Uri microsoftLogin = new Uri("https://login.microsoftonline.com/");
            string tenantID = "2e83cc45-652e-cccc-a85a-80c981c30c09"; // Put the Azure AD Tenant ID from above here.

            // The authority to ask for a token: your azure active directory.
            string authority = new Uri(microsoftLogin, tenantID).AbsoluteUri;
            AuthenticationContext authenticationContext = new AuthenticationContext(authority);
            ClientCredential clientCredential = new ClientCredential(clientID, clientSecret);

            // Picks up the bearer token.
            AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(graphApiResource, clientCredential).Result;

            GraphServiceClient graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
                async (requestMessage) =>
                {
                    // This is adding a bearer token to the httpclient used in the requests.
                    requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", authenticationResult.AccessToken);
                }));

            var site = graphClient.Sites["contoso.sharepoint.com"].Request().GetAsync().Result;

更新:

using System;
using Microsoft.Graph;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Net.Http.Headers;

【讨论】:

  • 谢谢,我会努力的!你能给我提供使用语句吗?我看到“Microsoft.Identity.Client.ClientCredential”已过时,并且没有采用 2 个参数的构造函数。
  • 谢谢,我设法运行了代码,但我仍然面临同样的 500 内部服务器错误。 Azure AD 或 Sharepoint 上是否有任何可能导致此问题的内容? ServiceException: Code: generalException Message: An unspecified error has occurred. Inner error: AdditionalData: date: 2021-02-08T09:19:40 request-id: 235137f5-acc2-443b-9085-82e099fb9afc client-request-id: 235137f5-acc2-443b-9085-82e099fb9afc ClientRequestId: 235137f5-acc2-443b-9085-82e099fb9afc
  • 在graph explorer中测试这个API能正常取值吗? 5xx http 状态码通常是服务器问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2018-09-30
  • 2021-03-25
  • 1970-01-01
相关资源
最近更新 更多