Example #1
0
        public static TwitterStatusMessageResponse PostTweet(object OAuthValue, string tweet)
        {
            TwitterOAuthData twitter = OAuthValue as TwitterOAuthData;

            // Check whether the OAuth data is valid
            if (twitter != null && twitter.IsValid)
            {
                // Gets an instance of TwitterService based on the OAuth data
                TwitterService service = twitter.GetService();
                // Post tweet
                TwitterStatusMessageResponse response = service.Statuses.PostStatusMessage(tweet);
                // Return response if successful
                return(response);
            }
            return(null);
        }
Example #2
0
        public static IEnumerable <TwitterStatusMessage> GetLastestTwitterPosts(object OAuthValue)
        {
            // Check whether the OAuth data is valid
            TwitterOAuthData twitter = OAuthValue as TwitterOAuthData;

            // Check whether the OAuth data is valid
            if (twitter != null && twitter.IsValid)
            {
                // Gets an instance of TwitterService based on the OAuth data
                TwitterService service = twitter.GetService();

                // Get information about the authenticated user
                TwitterUser user = service.Users.GetUser(twitter.Id).Body;

                // Get recent status messages (tweets) from the authenticated user
                return(service.Statuses.GetUserTimeline(user.Id).Body);
            }
            return(null);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            Callback         = Request.QueryString["callback"];
            ContentTypeAlias = Request.QueryString["contentTypeAlias"];
            PropertyAlias    = Request.QueryString["propertyAlias"];

            // Get the prevalue options
            TwitterOAuthPreValueOptions options = TwitterOAuthPreValueOptions.Get(ContentTypeAlias, PropertyAlias);

            if (!options.IsValid)
            {
                Content.Text += "<p><strong>ContentTypeAlias</strong> " + ContentTypeAlias + "</p>";
                Content.Text += "<p><strong>PropertyAlias</strong> " + PropertyAlias + "</p>";
                Content.Text += "Hold on now! The options of the underlying prevalue editor isn't valid.";
                return;
            }

            // Configure the OAuth client based on the options of the prevalue options
            TwitterOAuthClient client = new TwitterOAuthClient {
                ConsumerKey    = options.ConsumerKey,
                ConsumerSecret = options.ConsumerSecret,
                Callback       = Request.Url.ToString()
            };

            // Check whether the user has denied the app
            if (HasUserDenied)
            {
                Session.Remove(DeniedToken);
                Content.Text = "<div class=\"error\">Error: The app was denied access to your account.</div>";
                return;
            }

            #region OAuth 1.0a - Step 3

            if (OAuthToken != null)
            {
                // Grab the request token from the session
                OAuthRequestToken token = Session[OAuthToken] as OAuthRequestToken;

                // Check whether the requets token was found in the current session
                if (token == null)
                {
                    Content.Text = "<div class=\"error\">An error occured. Timeout?</div>";
                    return;
                }

                // Update the token and token secret
                client.Token       = token.Token;
                client.TokenSecret = token.TokenSecret;

                // Now get the access token
                try {
                    OAuthAccessToken accessToken = client.GetAccessToken(OAuthVerifier);
                    client.Token       = accessToken.Token;
                    client.TokenSecret = accessToken.TokenSecret;
                } catch (Exception) {
                    Content.Text = "<div class=\"error\">Unable to retrieve access token from <b>Twitter.com</b>.</div>";
                    return;
                }

                try {
                    // Initialize an instance of TwitterService
                    TwitterService service = TwitterService.CreateFromOAuthClient(client);

                    // Get information about the server
                    TwitterUser user = service.Account.VerifyCredentials().Body;

                    Content.Text += "<p>Hi <strong>" + (String.IsNullOrEmpty(user.Name) ? user.ScreenName : user.Name) + "</strong></p>";
                    Content.Text += "<p>Please wait while you're being redirected...</p>";

                    // Set the callback data
                    TwitterOAuthData data = new TwitterOAuthData {
                        Id                = user.Id,
                        ScreenName        = user.ScreenName,
                        Name              = String.IsNullOrEmpty(user.Name) ? "" : user.Name,
                        Avatar            = user.ProfileImageUrlHttps,
                        ConsumerKey       = client.ConsumerKey,
                        ConsumerSecret    = client.ConsumerSecret,
                        AccessToken       = client.Token,
                        AccessTokenSecret = client.TokenSecret
                    };

                    // Update the UI and close the popup window
                    Page.ClientScript.RegisterClientScriptBlock(GetType(), "callback", String.Format(
                                                                    "self.opener." + Callback + "({0}); window.close();",
                                                                    data.Serialize()
                                                                    ), true);
                } catch (TwitterException ex) {
                    Content.Text = "<div class=\"error\">Error in the communication with Twitter.com<br /><br />" + ex.Message + " (Code: " + ex.Code + ")</div>";
                } catch (Exception) {
                    Content.Text = "<div class=\"error\">Error in the communication with Twitter.com</div>";
                }

                return;
            }

            #endregion

            #region OAuth 1.0a - Step 1

            // Get a request token from the Twitter API
            OAuthRequestToken requestToken = client.GetRequestToken();

            // Save the token information to the session so we can grab it later
            Session[requestToken.Token] = requestToken;

            // Redirect the user to the authentication page at Twitter.com
            Response.Redirect(requestToken.AuthorizeUrl);

            #endregion
        }