示例#1
0
        /**
         * In this function we firstly load the auth pin authorizer with the application credentials.
         * We also load the login twitter page in order to obtain the pin.
         */
        void OAuthPage_Loaded(object sender, RoutedEventArgs e)
        {
            //Auth definition with our application credentials
            auth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = Constants.getConsumerKey(),
                    ConsumerSecret = Constants.getConsumerSecret()
                },
                UseCompression = true,
                //Browser authentication webpage
                GoToTwitterAuthorization = pageLink =>
                    Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                        () => OAuthWebBrowser.Navigate(new Uri(pageLink, UriKind.Absolute)))
            };

            auth.BeginAuthorize(resp =>
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    switch (resp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            break;
                        case TwitterErrorStatus.RequestProcessingException:
                        case TwitterErrorStatus.TwitterApiError:
                            //new MessageDialog(resp.Error.ToString(), resp.Message).ShowAsync();
                            break;
                    }
                }));
        }
        private void DoPinAuth()
        {
            m_pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "",
                    ConsumerSecret = ""
                },
                UseCompression = true,
                GoToTwitterAuthorization = pageLink =>
                    Dispatcher.BeginInvoke(() => WebBrowser.Navigate(new Uri(pageLink)))
            };

            m_pinAuth.BeginAuthorize(resp =>
                Dispatcher.BeginInvoke(() =>
                {
                    switch (resp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            break;
                        case TwitterErrorStatus.TwitterApiError:
                        case TwitterErrorStatus.RequestProcessingException:
                            MessageBox.Show(
                                resp.Error.ToString(),
                                resp.Message,
                                MessageBoxButton.OK);
                            break;
                    }
                }));

            m_twitterCtx = new TwitterContext(m_pinAuth, "https://api.twitter.com/1/", "https://search.twitter.com/");
        }
        public void BeginAuthorize_Returns_If_Already_Authorized()
        {
            var oAuthMock = new Mock<IOAuthTwitter>();
            TwitterAsyncResponse<object> twitterResp = null;
            var pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "consumerkey",
                    ConsumerSecret = "consumersecret",
                    OAuthToken = "oauthtoken",
                    AccessToken = "accesstoken"
                },
                OAuthTwitter = oAuthMock.Object,
                GetPin = () => "1234567",
                GoToTwitterAuthorization = link => { }
            };

            pinAuth.BeginAuthorize(resp => twitterResp = resp);

            oAuthMock.Verify(oauth =>
                oauth.GetRequestTokenAsync(It.IsAny<Uri>(), It.IsAny<Uri>(), "oob", AuthAccessType.NoChange, false, It.IsAny<Action<string>>(), It.IsAny<Action<TwitterAsyncResponse<object>>>()),
                Times.Never());
            Assert.Null(twitterResp);
        }
        public void BeginAuthorize_Requires_GoToTwitterAuthorization_Handler()
        {
            var pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials(),
                OAuthTwitter = new OAuthTwitterMock(),
                GetPin = () => "1234567",
            };

            var ex = Assert.Throws<InvalidOperationException>(() => pinAuth.BeginAuthorize(resp => { }));

            Assert.True(ex.Message.Contains("GoToTwitterAuthorization"));
        }
        public void BeginAuthorize_Invokes_AuthorizationCompleteCallback()
        {
            var pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials(),
                OAuthTwitter = new OAuthTwitterMock(),
                GetPin = () => "1234567",
                GoToTwitterAuthorization = link => { }
            };
            TwitterAsyncResponse<object> twitterResp = null;
            pinAuth.BeginAuthorize(resp => twitterResp = resp);

            Assert.NotNull(twitterResp);
        }
        public void BeginAuthorize_Gets_Request_Token()
        {
            var oauthRequestTokenUrl = new Uri("https://api.twitter.com/oauth/request_token");
            var oauthAuthorizeUrl = new Uri("https://api.twitter.com/oauth/authorize");
            var oAuthMock = new Mock<IOAuthTwitter>();
            var pinAuth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials(),
                OAuthTwitter = oAuthMock.Object,
                GetPin = () => "1234567",
                GoToTwitterAuthorization = link => { }
            };

            pinAuth.BeginAuthorize(resp => { });

            oAuthMock.Verify(oAuth =>
                oAuth.GetRequestTokenAsync(oauthRequestTokenUrl, oauthAuthorizeUrl, "oob", AuthAccessType.NoChange, false, It.IsAny<Action<string>>(), It.IsAny<Action<TwitterAsyncResponse<object>>>()),
                Times.Once());
        }
        void Login_Loaded(object sender, RoutedEventArgs e)
        {
            auth = new PinAuthorizer
            {
                Credentials = new InMemoryCredentials
                {
                    ConsumerKey = "<add your consumer key here>",
                    ConsumerSecret = "<add your consumer secret here>"
                },
                UseCompression = true,
                GoToTwitterAuthorization = pageLink =>
                    Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                        () => OAuthWebBrowser.Navigate(
                            new Uri(pageLink, UriKind.Absolute))).AsTask().Wait()
            };

            auth.BeginAuthorize(resp =>
                Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    switch (resp.Status)
                    {
                        case TwitterErrorStatus.Success:
                            break;
                        case TwitterErrorStatus.RequestProcessingException:
                        case TwitterErrorStatus.TwitterApiError:
                            new MessageDialog(resp.Error.ToString(),
                                resp.Message).ShowAsync().AsTask().Wait();
                            break;
                    }
                }).AsTask().Wait());
        }