Represents a Twitter OAuth access token.
Call Save when an OAuth access token is obtained from Twitter. Save saves the token to a file in the user's profile. Call TryLoad to determine whether a token has been obtained from Twitter.

(Confusingly, a Twitter "token" consists of both a token string and a secret string. The same word is used for both a single string and a pair of strings.)

Inheritance: Object
Example #1
0
        //*************************************************************************
        //  Constructor: TwitterAuthorizationManager()
        //
        /// <summary>
        /// Initializes a new instance of the <see
        /// cref="TwitterAuthorizationManager" /> class.
        /// </summary>
        ///
        /// <param name="twitterAuthorizationControl">
        /// The TwitterAuthorizationControl owned by the parent dialog.  This class
        /// manages the control's <see cref="TwitterAuthorizationControl.Status" />
        /// property.
        /// </param>
        //*************************************************************************

        public TwitterAuthorizationManager
        (
            TwitterAuthorizationControl twitterAuthorizationControl
        )
        {
            m_oTwitterAuthorizationControl = twitterAuthorizationControl;

            // If the user has already authorized, he doesn't need to do so again.

            m_oTwitterAuthorizationControl.Status =
                TwitterAccessToken.Exists() ?
                TwitterAuthorizationStatus.HasTwitterAccountAuthorized :
                TwitterAuthorizationStatus.HasTwitterAccountNotAuthorized;

            AssertValid();
        }
        BeforeGetNetwork()
        {
            AssertValid();

            // TwitterAccessToken caches the access token it reads from disk.  Make
            // sure the latest access token is read.

            TwitterAccessToken oTwitterAccessToken = new TwitterAccessToken();

            // A network should never be requested if the access token hasn't been
            // saved yet.

            String sToken, sSecret;

            if (!oTwitterAccessToken.TryLoad(out sToken, out sSecret))
            {
                throw new Exception("Twitter access token not set.");
            }

            m_oTwitterUtil = new TwitterUtil(sToken, sSecret,
                                             HttpNetworkAnalyzerBase.UserAgent,
                                             HttpNetworkAnalyzerBase.HttpWebRequestTimeoutMs);
        }
    SetUp()
    {
        m_oTwitterAccessToken = new TwitterAccessToken();

        DeleteAccessTokenFile();
    }
Example #4
0
        AuthorizeIfRequested()
        {
            AssertValid();

            switch (m_oTwitterAuthorizationControl.Status)
            {
            case TwitterAuthorizationStatus.HasTwitterAccountAuthorized:

                return(true);

            case TwitterAuthorizationStatus.HasTwitterAccountNotAuthorized:

                TwitterAccessToken.Delete();

                // Continue below.

                break;

            default:

                Debug.Assert(false);
                break;
            }

            // Get a Twitter request token.

            oAuthTwitter oTwitterAuth = new oAuthTwitter(
                HttpNetworkAnalyzerBase.UserAgent,
                HttpNetworkAnalyzerBase.HttpWebRequestTimeoutMs);

            Uri oAuthorizationUri = new Uri(oTwitterAuth.AuthorizationLinkGet());

            String sRequestToken = HttpUtility.ParseQueryString(
                oAuthorizationUri.Query)["oauth_token"];

            Debug.Assert(!String.IsNullOrEmpty(sRequestToken));

            // Open the Twitter authorization page.

            Process.Start(oAuthorizationUri.ToString());

            // Tell the user that the Twitter authorization page has been opened in
            // a browser window, and get the PIN that Twitter provides after
            // authorization is complete.

            TwitterAuthorizingDialog oTwitterAuthorizingDialog =
                new TwitterAuthorizingDialog();

            if (oTwitterAuthorizingDialog.ShowDialog(
                    m_oTwitterAuthorizationControl.ParentForm) != DialogResult.OK)
            {
                return(false);
            }

            // Convert the request token to an access token.

            oTwitterAuth.Token = sRequestToken;

            oTwitterAuth.AccessTokenGet(
                sRequestToken, oTwitterAuthorizingDialog.Pin);

            Debug.Assert(!String.IsNullOrEmpty(oTwitterAuth.Token));
            Debug.Assert(!String.IsNullOrEmpty(oTwitterAuth.TokenSecret));

            // Save the access token.

            TwitterAccessToken oTwitterAccessToken = new TwitterAccessToken();

            oTwitterAccessToken.Save(oTwitterAuth.Token, oTwitterAuth.TokenSecret);

            return(true);
        }
    BeforeGetNetwork()
    {
        AssertValid();

        // TwitterAccessToken caches the access token it reads from disk.  Make
        // sure the latest access token is read.

        TwitterAccessToken oTwitterAccessToken = new TwitterAccessToken();

        // A network should never be requested if the access token hasn't been
        // saved yet.

        String sToken, sSecret;

        if ( !oTwitterAccessToken.TryLoad(out sToken, out sSecret) )
        {
            throw new Exception("Twitter access token not set.");
        }

        m_oTwitterUtil = new TwitterUtil(sToken, sSecret,
            HttpNetworkAnalyzerBase.UserAgent,
            HttpNetworkAnalyzerBase.HttpWebRequestTimeoutMs);
    }
    AuthorizeIfRequested()
    {
        AssertValid();

        switch (m_oTwitterAuthorizationControl.Status)
        {
            case TwitterAuthorizationStatus.NoTwitterAccount:

                // Delete any access token that exists.

                TwitterAccessToken.Delete();
                return (true);

            case TwitterAuthorizationStatus.HasTwitterAccountAuthorized:

                return (true);

            case TwitterAuthorizationStatus.HasTwitterAccountNotAuthorized:

                TwitterAccessToken.Delete();

                // Continue below.

                break;

            default:

                Debug.Assert(false);
                break;
        }

        // Get a Twitter request token.

        oAuthTwitter oTwitterAuth = new oAuthTwitter(
            HttpNetworkAnalyzerBase.UserAgent,
            HttpNetworkAnalyzerBase.HttpWebRequestTimeoutMs);

        Uri oAuthorizationUri = new Uri( oTwitterAuth.AuthorizationLinkGet() );

        String sRequestToken = HttpUtility.ParseQueryString(
            oAuthorizationUri.Query)["oauth_token"];

        Debug.Assert( !String.IsNullOrEmpty(sRequestToken) );

        // Open the Twitter authorization page.

        Process.Start( oAuthorizationUri.ToString() );

        // Tell the user that the Twitter authorization page has been opened in
        // a browser window, and get the PIN that Twitter provides after
        // authorization is complete.

        TwitterAuthorizingDialog oTwitterAuthorizingDialog =
            new TwitterAuthorizingDialog();

        if (oTwitterAuthorizingDialog.ShowDialog(
            m_oTwitterAuthorizationControl.ParentForm) != DialogResult.OK)
        {
            return (false);
        }

        // Convert the request token to an access token.

        oTwitterAuth.Token = sRequestToken;

        oTwitterAuth.AccessTokenGet(
            sRequestToken, oTwitterAuthorizingDialog.Pin);

        Debug.Assert( !String.IsNullOrEmpty(oTwitterAuth.Token) );
        Debug.Assert( !String.IsNullOrEmpty(oTwitterAuth.TokenSecret) );

        // Save the access token.

        TwitterAccessToken oTwitterAccessToken = new TwitterAccessToken();
        oTwitterAccessToken.Save(oTwitterAuth.Token, oTwitterAuth.TokenSecret);

        return (true);
    }