這個問題在這里已經有了答案: 如何在 C# 中獲取 OAuth 2.0 身份驗證令牌 (9 個回答) 2 天前關閉。
我只想獲取訪問令牌并將其傳遞到新請求中,但出現錯誤: 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;
....
uj5u.com熱心網友回復:
嘗試這個
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
uj5u.com熱心網友回復:
我沒有使用過 RestSharp,但查看了反序列化回應內容所需的檔案。
您可以使用 Execute<T>() 版本來執行此操作,該版本回傳 IRestResponse<T> ,其中包含所請求物件的 T 資料成員。
IRestResponse<TokenResponseClass> response = client.Execute<TokenResponseClass>(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.Data.AccessToken;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/355970.html
下一篇:使用bash-c逐行決議
