コード例 #1
0
        public OAuthToken getAuthToken(string autorizationUrl, string username, string password)
        {
            var        sync       = new AutoResetEvent(false);
            OAuthToken oauthToken = null;
            var        tokenFile  = 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(OAUTH_CONSUMER_KEY, 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);
        }