Exemplo n.º 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.
            // Otherwise, assume he doesn't have a Twitter account.

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

            AssertValid();
        }
Exemplo n.º 2
0
        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();
            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);
        }