restsharp / RestSharp

Simple REST and HTTP API Client for .NET
https://restsharp.dev
Apache License 2.0
9.62k stars 2.34k forks source link

Add support for OAuth authentication #14

Closed brianly closed 13 years ago

brianly commented 14 years ago

I had a quick look and didn't see any references to support for this. OAuth is multi-legged in terms of setup, but once you have the right credentials at the client side you can send over as headers in a similar way to basic authentication. There are OAuth samples for .NET at http://oauth.net/code/ but I think they make the protocol feel overly complicated.

I would use for a Yammer API client I am building, maybe I will get some time to hack this up for inclusion in RestSharp.

johnsheehan commented 14 years ago

Thanks for creating this ticket. This is definitely on the radar (there's been some work on it already) but it will be after 1.0. Don't worry, the version number is mostly meaningless, I hope 2.0 comes quickly after 1.0 is out.

joergbattermann commented 14 years ago

+1 on oAuth Support :)

prabirshrestha commented 14 years ago

i got the oauth2 authentication done. there are many ways we can do oauth2 authentication. the one im gonna show here will be using the URI Query Parameter more details here http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-5.1.2

since oauth2 has lots of authentciation methods. i actually created an abstract class for it.

public abstract class OAuth2Authenticator : IAuthenticator {

region Implementation of IAuthenticator

public abstract void Authenticate(RestRequest request);

#endregion

}

then i create specific oauth2 authenticators that is uri paramater.

/// /// based on http://tools.ietf.org/html/draft-ietf-oauth-v2-10#section-5.1.2 /// public class OAuth2UriQueryParamaterAuthenticator : OAuth2Authenticator { private readonly string _accessToken;

public OAuth2UriQueryParamaterAuthenticator(string accessToken)
{
    _accessToken = accessToken;
}

#region Overrides of OAuth2Authenticator

public override void Authenticate(RestRequest request)
{
    request.AddParameter("oauth_token", _accessToken, ParameterType.GetOrPost);
}

#endregion

}

so i want to access.. lets say facebook graph api which uses oauth2 for authentication.

var client = new RestClient("https://graph.facebook.com/me"); client.Authenticator = new OAuth2UriQueryParamaterAuthenticator( "acces_token"); var request = new RestRequest(); var respone = client.Execute(request);

        Console.WriteLine(respone.Content);

thats all.

actually facebook requires "access_token" parameter. but since it wants to maintain standards it also supports the "oauth_token" parameter instead of "access_token". <:-) that makes our life easier..

johnsheehan commented 13 years ago

oauth1 and 2 now supported in sync methods thanks to contributions from prabir and danielcrenna. async will follow post release 1