Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to get only the access token using RestSharp

I want to get only the access token and pass it in a new request but I am getting an error "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;

....

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try this

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading