Exemplo n.º 1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            // Create Twitter API
            TwitterApi api = new TwitterApi(
                new ApiCallOptions
                {
                    AuthorizationCallbackUri = new Uri(
                        this.Request.Url, 
                        new VPathResolver().Resolve("~/User.aspx"))
                });

            try
            {
                // Load current user
                // If the user has not yet connected to Twitter, this will
                // throw an AuthorizationRequiredException, handled below
                CachingContainer<ExtendedUser> extendedUserInfoCache
                    = this.Session[UserInfoKey] as CachingContainer<ExtendedUser>;
                
                if (extendedUserInfoCache == null)
                    this.Session[UserInfoKey] = extendedUserInfoCache 
                        = new CachingContainer<ExtendedUser>();

                this.TwitterUser = extendedUserInfoCache.Load(
                    TimeSpan.FromMinutes(5),
                    () => 
                    {
                        ExtendedUser user;
                        if (api.VerifyCredentials(out user))
                            return user;
                        else
                            throw new BadCredentialsException();
                    });

                // Load user timeline
                CachingContainer<IList<Status>> userTimelineCache
                    = this.Session[UserTimelineKey] as CachingContainer<IList<Status>>;

                if (userTimelineCache == null)
                    this.Session[UserTimelineKey] = userTimelineCache
                        = new CachingContainer<IList<Status>>();

                this.UserTimeline = userTimelineCache.Load(
                    TimeSpan.FromSeconds(30),
                    () => api.UserTimeline());
            }
            catch (BadCredentialsException)
            {
                this.Response.Redirect("~/Disconnect.aspx?error=badcredentials", true);
            }
        }
Exemplo n.º 2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!"POST".Equals(this.Request.HttpMethod, StringComparison.OrdinalIgnoreCase))
                throw new HttpException(405, "Method not allowed");

            string status = this.Request.Params["status"];

            if (string.IsNullOrEmpty(status))
                throw new HttpException(400, "Bad Request");

            // Create Twitter API
            TwitterApi api = new TwitterApi(
                new ApiCallOptions
                {
                    AuthorizationCallbackUri = new Uri(
                        this.Request.Url,
                        new VPathResolver().Resolve(
                            "~/UpdateStatus.aspx?status=" + status))
                });

            try
            {
                // Call Twitter API
                api.UpdateStatus(status);
            }
            catch (BadCredentialsException)
            {
                this.Response.Redirect("~/Disconnect.aspx?error=badcredentials", true);
            }
            catch (Exception)
            {
                // The API call didn't work out (or something else bad happened)
                this.Response.Redirect("~/User.aspx?error=unknown", true);
            }

            // Update must have been successful
            // Force reload of affected caches
            this.Session.Remove(UserPage.UserInfoKey);
            this.Session.Remove(UserPage.UserTimelineKey);

            // Redirect to User page
            this.Response.Redirect("~/User.aspx", true);
        }