简体   繁体   English

使用 MS Graph 站点 API 和 C# 时出现内部服务器错误

[英]Internal Server Error while using MS Graph Site API with C#

I'm trying to search file on SharePoint using Graph API.我正在尝试使用图形 API 在 SharePoint 上搜索文件。 Getting the correct result on Microsoft graph-explorer but when I try with C# Getting error Status Code: 500 Reason Phrase:Internal Server ErrorMicrosoft graph-explorer上获得正确的结果,但是当我尝试使用 C# 时出现错误Status Code: 500 Reason Phrase:Internal Server Error

Response Details as below:回复详情如下:

{
"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# Code I'm using 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;

Any pointers where I'm going wrong?任何指示我哪里出错了?

Try using the below code.尝试使用下面的代码。 Looks like your code is getting serialized twice.看起来您的代码被序列化了两次。 So I just removed the serialization step and it worked.所以我只是删除了序列化步骤并且它起作用了。

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;

You can always test the API requests using fiddler .您始终可以使用fiddler测试 API 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ms-graph API 与 C#; 如何修复此驱动项错误 - ms-graph API with C#; how to fix this driveitem error 使用 MS Graph 读取用户电子邮件 API C# - Reading user emails using MS Graph API C# 500:尝试从C#控制台调用REST API时发生内部服务器错误 - 500: internal server error while trying to call a REST api from C# console 更新一个问题以使用Rest API C#替换服务器并获得500 Internal Server Error - Update an issue to redmine server using Rest API C# and getting 500 Internal Server Error 发布数据时Webresponse上的内部服务器错误,但可以使用浏览器C#进行工作 - Internal server error on webresponse while posting data but works using a browser C# C# Web API - 内部服务器错误 500 - C# Web API - Internal Server Error 500 500 px API与C#引发500内部服务器错误 - 500 px api with C# throwing 500 Internal Server Error C#内部服务器错误 - C# Internal Server Error 使用 microsoft graph api c# 创建在线会议时出现 404 错误,而无需登录 AzureActiveDirectory - 404 error while creating Online Meeting using microsoft graph api c# without login into AzureActiveDirectory 在 Web API 中使用异步等待时出现 500 内部服务器错误 - 500 internal server error while using async await in web API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM