Exemple #1
0
        public void Authorize_Gets_Access_Token()
        {
            string       screenName = "JoeMayo";
            string       userID     = "123";
            const string PinCode    = "1234567";
            const string AuthToken  = "token";
            const string AuthLink   = "https://authorizationlink?oauth_token=" + AuthToken;
            var          pinAuth    = new PinAuthorizer {
                Credentials = new InMemoryCredentials()
            };
            var oAuthMock = new Mock <IOAuthTwitter>();

            oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny <string>(), It.IsAny <string>(), "oob", false, AuthAccessType.NoChange))
            .Returns(AuthLink);
            oAuthMock.Setup(oAuth => oAuth.AccessTokenGet(AuthToken, PinCode, It.IsAny <string>(), string.Empty, out screenName, out userID));
            pinAuth.OAuthTwitter = oAuthMock.Object;
            pinAuth.GetPin       = () => PinCode;
            string destinationUrl = string.Empty;

            pinAuth.GoToTwitterAuthorization = link => destinationUrl = link;

            pinAuth.Authorize();

            oAuthMock.Verify(oauth => oauth.AccessTokenGet(AuthToken, PinCode, It.IsAny <string>(), string.Empty, out screenName, out userID), Times.Once());
            Assert.Equal(screenName, pinAuth.ScreenName);
            Assert.Equal(userID, pinAuth.UserId);
            Assert.Equal(AuthLink, destinationUrl);
        }
Exemple #2
0
        private static TwitterContext CreateContext(string accessToken, string oAuthToken)
        {
            var auth = new PinAuthorizer()
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = "rgRMroMnWQ8IoUNYcNIX0BsLV",
                    ConsumerSecret = "vfX2T7V9kwmwDzCyBW8ZP91jElRhGnCNnG6fbfcvz1ysOrt8Bz",
                    AccessToken    = accessToken,
                    OAuthToken     = oAuthToken
                },
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    Console.WriteLine(
                        "\nAfter authorizing this application, Twitter " +
                        "will give you a 7-digit PIN Number.\n");
                    Console.Write("Enter the PIN number here: ");

                    var pin = Console.ReadLine();

                    return(pin);
                }
            };

            auth.AuthAccessType = AuthAccessType.Write;
            auth.Authorize();

            return(new TwitterContext(auth));
        }
Exemple #3
0
        public ITwitterAuthorizer Auth()
        {
            var credentials = new InMemoryCredentials
            {
                ConsumerKey    = this.consumerKey,
                ConsumerSecret = this.consumerSecret
            };

            if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(oAuthToken))
            {
                credentials.AccessToken = this.accessToken;
                credentials.OAuthToken  = this.oAuthToken;
            }

            var auth = new PinAuthorizer
            {
                Credentials              = credentials,
                UseCompression           = true,
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n");
                    Console.Write("Enter the PIN number here: ");
                    return(Console.ReadLine());
                }
            };

            auth.Authorize();

            return(auth);
        }
Exemple #4
0
        public void Authorize_Requires_Credentials()
        {
            var pinAuth = new PinAuthorizer();

            var ex = Assert.Throws <ArgumentNullException>(() => pinAuth.Authorize());

            Assert.Equal("Credentials", ex.ParamName);
        }
Exemple #5
0
        ITwitterAuthorizer PerformAuthorization()
        {
            // validate that credentials are present
            if (string.IsNullOrWhiteSpace(twitterConsumerKey) ||
                string.IsNullOrWhiteSpace(twitterConsumerSecret))
            {
                MessageBox.Show(@"Error while setting " +
                                "App.config/appSettings. \n\n" +
                                "You need to provide your twitterConsumerKey and twitterConsumerSecret in App.config \n" +
                                "Please visit http://dev.twitter.com/apps for more info.\n");

                return(null);
            }

            // configure the OAuth object
            var auth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = twitterConsumerKey,
                    ConsumerSecret = twitterConsumerSecret
                },
                UseCompression           = true,
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    // this executes after user authorizes, which begins with the call to auth.Authorize() below.

                    PinWindow pinw = new PinWindow();
                    pinw.Owner = this;
                    if (pinw.ShowDialog() == true)
                    {
                        return(pinw.Pin);
                    }
                    else
                    {
                        return("");
                    }
                }
            };

            // start the authorization process (launches Twitter authorization page).
            try
            {
                auth.Authorize();
            }
            catch (WebException ex)
            {
                /*MessageBox.Show("Unable to authroize with Twitter right now. Please check pin number", "Twitter Archive Eraser",
                 *  MessageBoxButton.OK, MessageBoxImage.Information);
                 */
                MessageBox.Show(ex.Message);

                return(null);
            }

            return(auth);
        }
Exemple #6
0
        public void Authorize_Requires_GoToTwitterAuthorization_Handler()
        {
            const string AuthLink = "https://authorizationlink";
            var          pinAuth  = new PinAuthorizer {
                Credentials = new InMemoryCredentials()
            };
            var oAuthMock = new Mock <IOAuthTwitter>();

            oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny <string>(), It.IsAny <string>(), "oob", false, AuthAccessType.NoChange))
            .Returns(AuthLink);
            pinAuth.OAuthTwitter = oAuthMock.Object;
            pinAuth.GetPin       = () => { return("1234567"); };

            var ex = Assert.Throws <InvalidOperationException>(() => pinAuth.Authorize());

            Assert.True(ex.Message.Contains("GoToTwitterAuthorization"));
        }
Exemple #7
0
        public void Authorize_Launches_Browser()
        {
            const string AuthLink = "https://authorizationlink";
            var          pinAuth  = new PinAuthorizer {
                Credentials = new InMemoryCredentials()
            };
            var oAuthMock = new Mock <IOAuthTwitter>();

            oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny <string>(), It.IsAny <string>(), "oob", false, AuthAccessType.NoChange))
            .Returns(AuthLink);
            pinAuth.OAuthTwitter = oAuthMock.Object;
            pinAuth.GetPin       = () => "1234567";
            string destinationUrl = string.Empty;

            pinAuth.GoToTwitterAuthorization = link => destinationUrl = link;

            pinAuth.Authorize();

            Assert.Equal(AuthLink, destinationUrl);
        }
Exemple #8
0
        public void Authorize_Returns_If_Already_Authorized()
        {
            var oAuthMock = new Mock <IOAuthTwitter>();
            var pinAuth   = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = "consumerkey",
                    ConsumerSecret = "consumersecret",
                    OAuthToken     = "oauthtoken",
                    AccessToken    = "accesstoken"
                },
                OAuthTwitter             = oAuthMock.Object,
                GetPin                   = () => "1234567",
                GoToTwitterAuthorization = link => { }
            };

            pinAuth.Authorize();

            oAuthMock.Verify(oauth =>
                             oauth.AuthorizationLinkGet(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), false, AuthAccessType.NoChange),
                             Times.Never());
        }
Exemple #9
0
        static ITwitterAuthorizer DoPinOAuth()
        {
            // validate that credentials are present
            if (ConfigurationManager.AppSettings["twitterConsumerKey"].IsNullOrWhiteSpace() ||
                ConfigurationManager.AppSettings["twitterConsumerSecret"].IsNullOrWhiteSpace())
            {
                Console.WriteLine("You need to set twitterConsumerKey and twitterConsumerSecret in App.config/appSettings. Visit http://dev.twitter.com/apps for more info.\n");
                Console.Write("Press any key to exit...");
                Console.ReadKey();
                return(null);
            }

            // configure the OAuth object
            var auth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey    = ConfigurationManager.AppSettings["twitterConsumerKey"],
                    ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"]
                },
                AuthAccessType = AuthAccessType.NoChange,
                //UseCompression = true,
                GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                GetPin = () =>
                {
                    // this executes after user authorizes, which begins with the call to auth.Authorize() below.
                    Console.WriteLine("\nAfter authorizing this application, Twitter will give you a 7-digit PIN Number.\n");
                    Console.Write("Enter the PIN number here: ");
                    return(Console.ReadLine());
                }
            };

            // start the authorization process (launches Twitter authorization page).
            auth.Authorize(true);
            return(auth);
        }