OWASP O2 Platform Blog

O2 Script: Twitter OAuth using TweetSharp

O2 Platform example of retriving the Twitter OAuthToken using the TweetSharp API

panel.clear();
var twitterAPI = new O2TwitterAPI();
Web.Https.ignoreServerSslErrors();
Func<string> getAuthorizationUrl =
    ()=>
    {
        "[O2TwitterAPI] retriving Authorization Url".info();

        var twitter = FluentTwitter.CreateRequest()
                                      .Configuration.UseHttps()
                                   .Authentication.GetRequestToken(twitterAPI.OAUTH_CONSUMER_KEY, twitterAPI.OAUTH_CONSUMER_SECRET);

        var response = twitter.Request();

        if (response.ResponseHttpStatusCode.neq(200))
        {
            "error in first twitter response".error();
            return null;
        }

        var unauthorizedToken = response.AsToken();

        var url =  FluentTwitter.CreateRequest().Authentication.GetAuthorizationUrl(unauthorizedToken.Token);
        "[O2TwitterAPI] Authorization Url retrived: {0}".info(url);
        return url;
    };


Func<string, string, string , OAuthToken> getAuthToken_2 =
    (autorizationUrl,username, password)=>
        {
            var sync = new AutoResetEvent(false);
            OAuthToken oauthToken = null;
            var tokenFile = twitterAPI.getTokenFileForUser(username);
            if (tokenFile.valid() && tokenFile.fileExists())
            {
                "[O2TwitterAPI] found cached token for user: {0}".info(username);
                return tokenFile.load<OAuthToken>();
            }

            var ie = autorizationUrl.ie();              // will open a new instance of IE
            //var ie = panel.add_IE();                  // will use an Embeded version of IE
            // configure IE to handle twitter redirect
            ie.beforeNavigate(
                (url)=> {
                            "[O2TwitterAPI] in BeforeNavigate for: {0}".debug(url);
                            if (url.starts("http://o2platform.com/?oauth_token="))
                            {
                                O2Thread.mtaThread(
                                    ()=>{
                                            var splitted = url.uri().Query.split("=");
                                            if(splitted.size()==2 && splitted[0] == "?oauth_token")
                                            {
                                                var token = splitted[1];
                                                "[O2TwitterAPI] Found Token: {0}".info(token);

                                                var twitter = FluentTwitter.CreateRequest()
                                                                   .Authentication.GetAccessToken(twitterAPI.OAUTH_CONSUMER_KEY, twitterAPI.OAUTH_CONSUMER_SECRET,token);

                                                oauthToken = twitter.Request().AsToken();

                                                if (oauthToken.notNull())
                                                {
                                                    oauthToken.saveAs(tokenFile);
                                                    "[O2TwitterAPI] OAuthToken saved to: {0}".info(tokenFile);
                                                }
                                            }
                                            sync.Set(); // continue
                                        });
                                "[O2TwitterAPI] Found O2Platform.com Twitter redirect, stoping IE request".debug();
                                return true;
                            }
                            return false;
                        });

            //perform login section

            ie.open(autorizationUrl);

            if (ie.hasLink("Sign out"))
                ie.link("Sign out").click();

            if(ie.hasField("session[password]") && ie.hasField("session[username_or_email]"))
            {
                ie.field("session[username_or_email]").value(username);
                ie.field("session[password]").value(password);
                ie.button("Allow").click();
            }

            if (sync.WaitOne(10000).isFalse())            // wait until the redirect has been processed
                "[O2TwitterAPI] OAuthToken request timeout".error();
            ie.close();
            return oauthToken;
        };


Func<string, ICredential, OAuthToken> getAuthToken =
    (autorizationUrl,credential)=>
        {
            if (credential.isNull())
                credential = ascx_AskUserForLoginDetails.ask();
            if (credential.isNull())
            {
                "[O2TwitterAPI] No credentials provided".error();
                return null;
            }
            return getAuthToken_2(autorizationUrl,credential.UserName, credential.Password);
        };

var autzUrl = getAuthorizationUrl();

// this will ask the user for a credential
var oAuthOToken = getAuthToken(autzUrl, null);
// this will use the test accounts included with he O2 API
//var oAuthOToken = getAuthToken_2(autzUrl, twitterAPI.TestAccount_UserName, twitterAPI.TestAccount_Password);

return     oAuthOToken;

//O2File:O2TwitterAPI.cs
//O2File:Watin_IE.cs
//using System.Threading;
//using TweetSharp.Twitter.Fluent;
//using TweetSharp.Twitter.Model;
//using TweetSharp.Model;
//using TweetSharp.Twitter.Extensions;
//O2Ref:TweetSharp.dll
//O2Ref:TweetSharp.Twitter.dll
//O2Ref:WatiN.Core.1x.dll
//O2Ref:O2_External_IE.dll
panel.clear(); var twitterAPI = new O2TwitterAPI(); Web.Https.ignoreServerSslErrors(); Func<string> getAuthorizationUrl = ()=> { “[O2TwitterAPI] retriving Authorization Url”.info();  var twitter = FluentTwitter.CreateRequest() .Configuration.UseHttps() .Authentication.GetRequestToken(twitterAPI.OAUTH_CONSUMER_KEY, twitterAPI.OAUTH_CONSUMER_SECRET); var response = twitter.Request(); if (response.ResponseHttpStatusCode.neq(200)) { “error in first twitter response”.error(); return null; } var unauthorizedToken = response.AsToken(); var url =  FluentTwitter.CreateRequest().Authentication.GetAuthorizationUrl(unauthorizedToken.Token); “[O2TwitterAPI] Authorization Url retrived: {0}”.info(url); return url; }; Func<string, string, string , OAuthToken> getAuthToken_2 = (autorizationUrl,username, password)=> { var sync = new AutoResetEvent(false); OAuthToken oauthToken = null; var tokenFile = twitterAPI.getTokenFileForUser(username); if (tokenFile.valid() && tokenFile.fileExists()) { “[O2TwitterAPI] found cached token for user: {0}”.info(username); return tokenFile.load<OAuthToken>(); } var ie = autorizationUrl.ie();              // will open a new instance of IE //var ie = panel.add_IE();                  // will use an Embeded version of IE // configure IE to handle twitter redirect ie.beforeNavigate( (url)=> { “[O2TwitterAPI] in BeforeNavigate for: {0}”.debug(url); if (url.starts(“http://o2platform.com/?oauth_token=&#8221;)) { O2Thread.mtaThread( ()=>{ var splitted = url.uri().Query.split(“=”); if(splitted.size()==2 && splitted[0] == “?oauth_token”) { var token = splitted[1]; “[O2TwitterAPI] Found Token: {0}”.info(token); var twitter = FluentTwitter.CreateRequest() .Authentication.GetAccessToken(twitterAPI.OAUTH_CONSUMER_KEY, twitterAPI.OAUTH_CONSUMER_SECRET,token); oauthToken = twitter.Request().AsToken(); if (oauthToken.notNull()) { oauthToken.saveAs(tokenFile); “[O2TwitterAPI] OAuthToken saved to: {0}”.info(tokenFile); } } sync.Set(); // continue }); “[O2TwitterAPI] Found O2Platform.com Twitter redirect, stoping IE request”.debug(); return true; } return false; }); //perform login section ie.open(autorizationUrl); if (ie.hasLink(“Sign out”)) ie.link(“Sign out”).click(); if(ie.hasField(“session[password]”) && ie.hasField(“session[username_or_email]”)) { ie.field(“session[username_or_email]”).value(username); ie.field(“session[password]”).value(password); ie.button(“Allow”).click(); } if (sync.WaitOne(10000).isFalse())            // wait until the redirect has been processed “[O2TwitterAPI] OAuthToken request timeout”.error(); ie.close(); return oauthToken; }; Func<string, ICredential, OAuthToken> getAuthToken = (autorizationUrl,credential)=> { if (credential.isNull()) credential = ascx_AskUserForLoginDetails.ask(); if (credential.isNull()) { “[O2TwitterAPI] No credentials provided”.error(); return null; } return getAuthToken_2(autorizationUrl,credential.UserName, credential.Password); }; var autzUrl = getAuthorizationUrl(); // this will ask the user for a credential var oAuthOToken = getAuthToken(autzUrl, null); // this will use the test accounts included with he O2 API //var oAuthOToken = getAuthToken_2(autzUrl, twitterAPI.TestAccount_UserName, twitterAPI.TestAccount_Password); return     oAuthOToken; //O2File:O2TwitterAPI.cs //O2File:Watin_IE.cs //using System.Threading; //using TweetSharp.Twitter.Fluent; //using TweetSharp.Twitter.Model; //using TweetSharp.Model; //using TweetSharp.Twitter.Extensions; //O2Ref:TweetSharp.dll //O2Ref:TweetSharp.Twitter.dll //O2Ref:WatiN.Core.1x.dll //O2Ref:O2_External_IE.dll

October 18, 2010 - Posted by | Interoperability, WatiN

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: