Exemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        IOAuthCredentials credentials = new SessionStateCredentials();

        if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
        {
            credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
            credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
        }

        auth = new WebAuthorizer
        {
            Credentials = credentials,
            PerformRedirect = authUrl => Response.Redirect(authUrl)
        };

        if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null)
        {
            auth.CompleteAuthorization(Request.Url);
        }

        if (string.IsNullOrWhiteSpace(credentials.ConsumerKey) ||
            string.IsNullOrWhiteSpace(credentials.ConsumerSecret))
        {
            // The user needs to set up the web.config file to include Twitter consumer key and secret.
            PrivateDataMultiView.SetActiveView(SetupTwitterConsumer);
        }
        else if (auth.IsAuthorized)
        {
            screenNameLabel.Text = auth.ScreenName;
            PrivateDataMultiView.SetActiveView(ViewPrivateUpdates);
            updateBox.Focus();
        }
        else
        {
            PrivateDataMultiView.SetActiveView(AuthorizeTwitter);
        }

        if (auth.IsAuthorized)
        {
            twitterCtx = new TwitterContext(auth);

            var search =
                (from srch in twitterCtx.Search
                 where srch.Type == SearchType.Search &&
                       srch.Query == "LINQ to Twitter"
                 select srch)
                .SingleOrDefault();

            TwitterListView.DataSource = search.Statuses;
            TwitterListView.DataBind(); 
        }
    }
Exemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            //var twitterCtx = new TwitterContext();

            IOAuthCredentials credentials = new SessionStateCredentials();

            if (credentials.ConsumerKey == null || credentials.ConsumerSecret == null)
            {
                credentials.ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
                credentials.ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
            }

            auth = new WebAuthorizer
            {
                Credentials = credentials,
                PerformRedirect = authUrl => Response.Redirect(authUrl)
            };

            if (!Page.IsPostBack)
            {
                auth.CompleteAuthorization(Request.Url);
            }

            else if (auth.IsAuthorized)
            {
                Label1.Text = "Congratulations, you're authorized";

            }

            var twitterCtx = new TwitterContext(auth);
            //twitterCtx = auth.IsAuthorized ? new TwitterContext(auth) : new TwitterContext();

            UserId = credentials.UserId;
            string token = credentials.OAuthToken;
            string sec = credentials.AccessToken;

            ////// show token and secret in index page after Authorization
            Label2.Text = token;
            Label3.Text = sec;
            Label4.Text = credentials.UserId;

            ////// creat twtUser object to store the info about the user
            TwitterUser twt = new TwitterUser(UserId, token, sec);

            ////// move user's info to another page throw Session
            Session["objTwt"] = twt;
        }
Exemplo n.º 3
0
        public void CompleteAuthorization_Gets_Access_Token()
        {
            string screenName = "JoeMayo";
            string userID = "123";
            const string Verifier = "1234567";
            const string AuthToken = "token";
            const string AuthLink = "https://authorizationlink?oauth_token=" + AuthToken + "&oauth_verifier=" + Verifier;
            var webAuth = new WebAuthorizer {Credentials = new InMemoryCredentials()};
            var oAuthMock = new Mock<IOAuthTwitter>();
            oAuthMock.Setup(oauth => oauth.GetUrlParamValue(It.IsAny<string>(), "oauth_verifier")).Returns(Verifier);
            oAuthMock.Setup(oauth => oauth.GetUrlParamValue(It.IsAny<string>(), "oauth_token")).Returns(AuthToken);
            oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny<string>(), It.IsAny<string>(), "https://authorizationlink", false, AuthAccessType.NoChange))
                     .Returns(AuthLink);
            oAuthMock.Setup(oAuth => oAuth.AccessTokenGet(AuthToken, Verifier, It.IsAny<string>(), string.Empty, out screenName, out userID));
            webAuth.OAuthTwitter = oAuthMock.Object;

            webAuth.CompleteAuthorization(new Uri(AuthLink));

            oAuthMock.Verify(oauth => oauth.AccessTokenGet(AuthToken, Verifier, It.IsAny<string>(), string.Empty, out screenName, out userID), Times.Once());
            Assert.Equal(screenName, webAuth.ScreenName);
            Assert.Equal(userID, webAuth.UserId);
        }
Exemplo n.º 4
0
        public void CompleteAuthorization_Requires_A_Uri()
        {
            var webAuth = new WebAuthorizer {Credentials = new InMemoryCredentials()};
            var oAuthMock = new Mock<IOAuthTwitter>();
            webAuth.OAuthTwitter = oAuthMock.Object;

            var ex = Assert.Throws<ArgumentNullException>(() => webAuth.CompleteAuthorization(null));

            Assert.Equal("callback", ex.ParamName);
        }
Exemplo n.º 5
0
        public void CompleteAuthorization_Requires_Credentials()
        {
            const string AuthLink = "https://authorizationlink";
            var webAuth = new WebAuthorizer();

            var ex = Assert.Throws<ArgumentNullException>(() => webAuth.CompleteAuthorization(new Uri(AuthLink)));

            Assert.Equal("Credentials", ex.ParamName);
        }