RestSharp 身体始终为空

Lam*_*fif 7 .net c# rest restsharp

我需要使用 RestSharp 调用 Post 服务

 var client = new RestClient(url);
 var request = new RestRequest(url,Method.POST);
 request.AddHeader("Content-type", "application/json");
 request.AddJsonBody(new  { grant_type = "client_credentials", client_id = clientIdParam, client_secret = appReg_clientSecret, ressource = _ressource }
               ); 
 var response = client.Post(request);
Run Code Online (Sandbox Code Playgroud)

问题是请求的body总是为null并且json body作为参数添加

在此输入图像描述

有任何想法吗?

小智 0

https://login.microsoftonline.com/{{tenantId}}/oauth2/token 似乎不接受 json,即使内容类型设置为“application/json”

我的解决方案:

var client = new RestClient("https://login.microsoftonline.com");
var request = new RestRequest("/{{tenantId}}/oauth2/token", Method.POST);

var requestBody = $"grant_type=client_credentials&client_id={clientIdParam}&client_secret={appReg_clientSecret}&resource={_resource}";

request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", requestBody, ParameterType.RequestBody);

var response = client.Execute(request);

Run Code Online (Sandbox Code Playgroud)