c# - 有没有办法使用 RestSharp 仅获取访问 token ?

标签 c# .net restsharp

我只想获取访问 token 并将其传递到新请求中,但收到错误:IRestResponse does not contain a definition for "AccessToken'

var client = new RestClient("https://localhost:5001/");
client.Timeout = -1;
var ClientID = "client";
var ClientSecret = "secret";
var Scope = "Api1";
var request = new RestRequest("connect/token", Method.POST);
request.AddParameter("grant_type", "client_credentials");
request.AddParameter("client_id", ClientID);
request.AddParameter("client_secret", ClientSecret);
request.AddParameter("scope", Scope);
IRestResponse response = client.Execute(request);
if (!response.IsSuccessful)
    {
       Console.WriteLine("Authorization token request failed with the following error: @{Error}", response.Content);
       throw new Exception(response.Content);
                           
    }
    else
    {
                                               
    var token = response.AccessToken;

....

最佳答案

试试这个

   var result = JsonConvert.DeserializeObject<dynamic>(response.Content); 
   var token = result.access_token //the name of the access token in your response can be different, change it to whatever suits your needs

关于c# - 有没有办法使用 RestSharp 仅获取访问 token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69898678/

相关文章:

restsharp - 不要在 RestSharp 中对参数进行编码

C#:字符串连接不起作用

.net - ASP.NET MVC - 获取 QueryString 值

c# - 全局静态类和方法不好吗?

c# - 是否可以在 .NET 中以彩色方式写入控制台?

c# - 具有应用程序见解的依赖关系日志记录不会记录响应

c# - 错误 : An expression tree may not contain a dynamic operation

c# - MS-Office 文档转换为 .PDF 格式

c# - 增加 Google PubSub 的截止时间超时

c# - 将 JSON 对象反序列化为 C# 对象