This project is read-only.
My English is poor................

OAuth access to use two classes.

OAuthAuthorizationService Class

OAuthAuthorizationService Class is Static Class.
You use OAuthAuthorizationService class until Access Token is obtained.


Method Description
GetRequestToken Gets the Request Token
GetAuthorizationURL Generate the User Authorization Url by Request Token.
GetAccessToken Gets the Access Token


GetRequestToken and GetAccessToken will return TokenResponse.
TokenResponse has two property.
TokenResponse.Token is token instance(RequestToken or AccessToken).

TokenResponse.AdditionalParameters is additional parameters as IEnumerable<Parameter>.

Example the case of twitter, TokenRequest that is returned by GetAccessToken has two parameters.
one of Parameter is "user_id".
one of Parameter is "screen_name".

Get Twitter AccessToken Example

Consumer consumer = new Consumer(
                twitterConsumerKey,
                twitterConsumerSecret);

//Get Request Token
RequestToken requestToken = OAuthAuthorizationService.GetRequestToken(
    consumer,
    "http://twitter.com/oauth/request_token",
    "http://twitter.com/",
    HttpMethodType.Get).Token as RequestToken;

//It is necessary you have authenticated to user. 
Process.Start(OAuthAuthorizationService.GetUserAuthorizationURL(
    "http://twitter.com/oauth/authorize",
    requestToken));

//You have inputted to user.
Console.Out.WriteLine("Please input verify...");

string verifier = Console.In.ReadLine().TrimEnd('\r', '\n');

//Get AccessToken
TokenResponse accessTokenResponse =  OAuthAuthorizationService.GetAccessToken(
    consumer,
   "http://twitter.com/oauth/access_token",
    "http://twitter.com/",
    requestToken,
    verifier,
    HttpMethodType.Post);
    
AccessToken accessToken = accessTokenResponse.Token;

//string user_id = (from p in accessTokenResponse.AdditionalParameters 
//                             where p.Key == "user_id" 
//                              select p).First().Value;
//
//string screen_name = (from p in accessTokenResponse.AdditionalParameters
//                             where p.Key == "screen_name"
//                              select p).First().Value;
//
//Console.WriteLine("Screen_Name : " + screen_name);

OAuthRequest Class

When you use OAuthRequest Class, AccessToken is required.

Use OAuthRequest.Create Method to create an instance of OAuthRequest.

You must at least call SetUrl and SetHttpMethod, after you create the instance of OAuthRequest.

OAuthResponse response = OAuthRequest.Create(consumer, accessToken)
                .SetIgnoreStatusCodeCondition(i => i >= 500)
                .SetHttpMethod(HttpMethodType.Get)
                .SetUrl("http://api.twitter.com/1/statuses/home_timeline.xml")
                .CommitRequest()
                .GetResponse();

If necessary, Add Parameters.
If necessary, Set Proxy.
If necessary, Set Realm.

When you don't want being thrown exception when network error happend that have specific Http Status Code, you can set IgnoreStatusCodeCondition
IgnoreStatusCodeCondition is the Predicate<int> delegate that defines the conditions of that you want to ignore network error that have specific HTTP StatusCode.
If network error happend that have specific Http Status Code, it will return OAuthResponse that IsSuccess property is set false and it won't throw Exception.
When you won't specified it,thrown exception is intact.

If necessary, Set RetryCount.
While retrying, if exeception is thrown, break retrying.
If you don't specify IgnoreStatusCodeCondition, this value will be Ignored.

OAuthResponse response = OAuthRequest.Create(consumer, accessToken)
                .SetIgnoreStatusCodeCondition(i => i >= 500)
                .SetHttpMethod(HttpMethodType.Get)
                .SetUrl("http://api.twitter.com/1/statuses/home_timeline.xml")
                .CommitRequest()
                .GetResponse();
//If network error happend with 500, return response normaly. but IsSuccess property of respose is *False*. 

//If network error happend with 400, WebException is thrown.             

OAuthResponse response = OAuthRequest.Create(consumer, accessToken)
                .SetIgnoreStatusCodeCondition(i => i >= 500)
                .SetHttpMethod(HttpMethodType.Get)
                .SetUrl("http://api.twitter.com/1/statuses/home_timeline.xml")
                .SetRetryCount(3)
                .CommitRequest()
                .GetResponse();
//If network error happend with 500, After retrying 3 times,return response normaly. but IsSuccess property of respose is *False*. 

//If network error happend with 400, WebException is thrown.             


You must call CommitRequest necessary work went.

After Call CommitRequest, GetReponse can be called.

Simple Usage

Get Twitter HomeTimeLine

var result = OAuthRequest.Create(consumer, accessToken)
                .SetHttpMethod(HttpMethodType.Get)
                .SetUrl("http://api.twitter.com/1/statuses/home_timeline.xml")
                .CommitRequest()
                .GetResponse()
                .ParseToXElement();
                
//ResponseBody of OAuthResponse can be gotten as string or XElement.                
var text = from e in result.Descendants("status")
           select
           s.Element("user").Element("screen_name").Value +
           " : " +
           s.Element("text").Value;

Tweet


//Twitter is not Supported Except 100 Continue
ServicePointManager.Expect100Continue = false;

string postText = Console.In.ReadLine().TrimEnd('\r', '\n');

var res =
    OAuthRequest.Create(consumer,accessToken)
    .SetHttpMethod(HttpMethodType.Post)
    .SetUrl("http://api.twitter.com/1/statuses/update.xml")
    .AddParameter(new Parameter(){Key="status",Value=postText})
    .CommitRequest()
    .GetResponse();

You can reuse Request of construction on the way.
Method operate DeepCopyInstance. so it is thread safety.

Recycle Request of construction on the way

//With proxy and if failed with Http Status Code over 500, Retry 3 times
var requestBase =
    OAuthRequest.Create(consumer, accessToken)
    .SetProxy(new WebProxy("http://proxy:8080"))
    .SetRetryCount(3)
    .SetIgnoreStatusCodeCondition(i => i >= 500)
    .AddParameter(new Parameter(){Key="count",Value="100"})
    .SetHttpMethod(HttpMethodType.Get);

//With Proxy and don't Retry
var readyHomeTimeLineRequest =
     requestBase
    .SetRetryCount(0)
    .SetUrl("http://api.twitter.com/1/statuses/home_timeline.xml")
    .CommitRequest();

//With proxy and if failed with Http Status Code over 500, Retry 3 times
var readyMentionTimeLineRequest =
    requestBase
    .SetUrl("http://api.twitter.com/1/statuses/mentions.xml")
    .CommitRequest();
    
    
//Without proxy and don't Retry
var readyMentionTimeLineRequest =
    requestBase
    .SetUrl("http://api.twitter.com/1/statuses/mentions.xml")
    .SetIgnoreStatusCodeCondition(null)
    .CommitRequest();

Last edited Jul 26, 2010 at 10:19 PM by ugaya40, version 11

Comments

No comments yet.