在 C# 中使用 MS Graph 站点 API 时出现内部服务器错误
Internal Server Error while using MS Graph Site API with C#
我正在尝试使用 Graph API 在 SharePoint 上搜索文件。在 Microsoft graph-explorer
上获得正确的结果但是当我尝试使用 C# 时出现错误 Status Code: 500
Reason Phrase:Internal Server Error
回复详情如下:
{
"error": {
"code": "System.NullReferenceException",
"message": "Object reference not set to an instance of an object.",
"innerError": {
"date": "2021-03-19T16:16:28",
"request-id": "f3092cda-b813-4507-928e-7e2de84fc88f",
"client-request-id": "f3092cda-b813-4507-928e-7e2de84fc88f"
}
}
}
我正在使用的 C# 代码
var searchTerm = "164377_07323_03122021_112659_TaxApp";
var headerStr = @"{{""requests"": [{{""entityTypes"": [""listItem""],""query"": {{""queryString"": ""{0}""}}}}]}}";
headerStr = string.Format(headerStr, searchTerm);
var headerJson = JsonConvert.SerializeObject(headerStr);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.AccessToken);
var content = new StringContent(headerJson, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/search/query", content).Result;
var postResult = response.Content.ReadAsStringAsync().Result;
我哪里出错了?
尝试使用以下代码。看起来您的代码被序列化了两次。所以我只是删除了序列化步骤并且它起作用了。
var searchTerm = "164377_07323_03122021_112659_TaxApp";
var headerStr = @"{{""requests"": [{{""entityTypes"": [""listItem""],""query"": {{""queryString"": ""{0}""}}}}]}}";
headerStr = string.Format(headerStr, searchTerm);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.AccessToken);
var content = new StringContent(headerStr, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/search/query", content).Result;
var postResult = response.Content.ReadAsStringAsync().Result;
您始终可以使用 fiddler.
测试 API 请求
我正在尝试使用 Graph API 在 SharePoint 上搜索文件。在 Microsoft graph-explorer
上获得正确的结果但是当我尝试使用 C# 时出现错误 Status Code: 500
Reason Phrase:Internal Server Error
回复详情如下:
{
"error": {
"code": "System.NullReferenceException",
"message": "Object reference not set to an instance of an object.",
"innerError": {
"date": "2021-03-19T16:16:28",
"request-id": "f3092cda-b813-4507-928e-7e2de84fc88f",
"client-request-id": "f3092cda-b813-4507-928e-7e2de84fc88f"
}
}
}
我正在使用的 C# 代码
var searchTerm = "164377_07323_03122021_112659_TaxApp";
var headerStr = @"{{""requests"": [{{""entityTypes"": [""listItem""],""query"": {{""queryString"": ""{0}""}}}}]}}";
headerStr = string.Format(headerStr, searchTerm);
var headerJson = JsonConvert.SerializeObject(headerStr);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.AccessToken);
var content = new StringContent(headerJson, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/search/query", content).Result;
var postResult = response.Content.ReadAsStringAsync().Result;
我哪里出错了?
尝试使用以下代码。看起来您的代码被序列化了两次。所以我只是删除了序列化步骤并且它起作用了。
var searchTerm = "164377_07323_03122021_112659_TaxApp";
var headerStr = @"{{""requests"": [{{""entityTypes"": [""listItem""],""query"": {{""queryString"": ""{0}""}}}}]}}";
headerStr = string.Format(headerStr, searchTerm);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResponse.AccessToken);
var content = new StringContent(headerStr, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/search/query", content).Result;
var postResult = response.Content.ReadAsStringAsync().Result;
您始终可以使用 fiddler.
测试 API 请求