Skip to content

Instantly share code, notes, and snippets.

@teocomi
Last active March 6, 2023 10:33
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save teocomi/9b65f59de827435000a3 to your computer and use it in GitHub Desktop.
Save teocomi/9b65f59de827435000a3 to your computer and use it in GitHub Desktop.
OAuth2 C# RestSharp
string url = "https://myurl.com";
string client_id = "client_id";
string client_secret = "client_secret";
//request token
var restclient = new RestClient(url);
RestRequest request = new RestRequest("request/oauth") {Method = Method.POST};
request.AddHeader("Accept", "application/json");
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("client_id", client_id);
request.AddParameter("client_secret", client_secret);
request.AddParameter("grant_type", "client_credentials");
var tResponse = restclient.Execute(request);
var responseJson = tResponse.Content;
var token = JsonConvert.DeserializeObject<Dictionary<string,object>>(responseJson)["access_token"].ToString();
return token.Length > 0 ? token : null;
@FisheyJay
Copy link

Thank you so much,... really saved my cookies here... whew... This was beating me up. Thanks again,... best regards JeanPierre (John) Fisher

@veerNeti
Copy link

Thank you very much, helped me too!!
I am not sure why RestSharp has less to no documentation on usage.

@ataccounts
Copy link

Still helpful! Tks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment