Skip to content
samo9789 edited this page Jan 29, 2011 · 3 revisions

How To Use sharpOauth

In order to use the library in your own .Net project, you must simply add a reference to the sharpOauth library in your project and add the using OAuth statement in your class.
using OAuth;

To make a call, you must first create an OAuthConfig.
OAuthConfig oauthConfig = new OAuthConfig("console");

The only parameter to this class is the debug mode you want, either "console" or "debug".

  1. "console" means that you want your debug to use Console.Writeline
  2. "debug" means your debug will show using Debug.Writeline.

In order for the library to be able to make webservice calls, you need to configure your OAuthClient.
Here is an example:
oauthConfig.SiteUrl = "http://mysite.com";
oauthConfig.OauthVersion = "1.0"; // The Library only handles HMAC-SHA1 at the moment oauthConfig.OauthSignatureMethod = "HMAC-SHA1"; oauthConfig.ConsumerKey = "enter consumer key here"; oauthConfig.ConsumerSecret = "enter consumer secret here"; oauthConfig.RequestTokenUrl = "https://domain.com/oauth/request_token"; oauthConfig.AccessTokenUrl = "https://domain.com/oauth/access_token"; oauthConfig.UserAuthorizationUrl = "https://domain.com/oauth/authorize";

Now you must instantiate your consumer object.
OAuthConsumer oauthConsumer = new OAuthConsumer(oauthConfig, "console");

  1. The first parameter is the oauthCOnfig object instantiated above.
  2. The second paramter is, again, the debug mode.

Once you have a consumer, you have access to three methods:
oauthConsumer.getRequestToken(); This method will open the authorization page of the distant application.

oauthConsumer.getAccessToken(pincode);

This method allows you to get an access token using a pincode (in case your application is a desktop client)

Finally, the most important method is
oauthConsumer.request("http://domain.com/1/service/action.xml", "GET", null, "PLAIN"); The first parameter is the URL to call.
The second is the httpMethod to use
The third is the list of parameters to be sent
The fourth is the format of the expected response. It can either be

  1. "PLAIN": a string is returned
  2. "DataSet": a dataset object will be returned
  3. "XML": an XML object will be returned

OAuth tokens

If you registered an application with an OAuth provider such as Twitter, you might have been given a token and tokenSecret set of strings. By using these keys, you are calling the webservice logged in as yourself... To use this with sharpOauth, simply add the two following lines in your OauthConfig declaration:
oauthConfig.OauthToken = "enter your token here"; oauthConfig.OauthTokenSecret = "enter your token secret here";

Clone this wiki locally