Skip to content

Instantly share code, notes, and snippets.

@teocomi
Last active April 18, 2018 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teocomi/fec12ab8ccb944d8a785 to your computer and use it in GitHub Desktop.
Save teocomi/fec12ab8ccb944d8a785 to your computer and use it in GitHub Desktop.
//using DotNetOpenAuth and RestSharp
WebServerClient client = new WebServerClient(
new AuthorizationServerDescription
{
TokenEndpoint = new Uri("https://myurl/oauth"),
ProtocolVersion = ProtocolVersion.V20
}, "client", "secret");
var token = client.GetClientAccessToken();
var restclient = new RestClient("https://myurl/endpoint");
RestRequest request = new RestRequest() { Method = Method.GET };
request.AddHeader("Authorization", "Bearer " + token.AccessToken);
request.AddHeader("Content-Type", "application/json");
restclient.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(token.AccessToken);
var response = restclient.Execute(request);
@kmorin
Copy link

kmorin commented Feb 16, 2016

//fixed with RestSharp only
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;

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