summaryrefslogtreecommitdiffstats
path: root/samples/OAuthConsumer/Twitter.aspx.cs
blob: 42ffa6e7f9d53e5ac86608a8e576178878d40bb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
namespace OAuthConsumer {
	using System;
	using System.Collections.Generic;
	using System.Configuration;
	using System.Linq;
	using System.Net;
	using System.Text;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.Xml.Linq;
	using System.Xml.XPath;
	using DotNetOpenAuth.ApplicationBlock;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OAuth;

	public partial class Twitter : System.Web.UI.Page {
		private AccessToken AccessToken {
			get { return (AccessToken)(Session["TwitterAccessToken"] ?? new AccessToken()); }
			set { Session["TwitterAccessToken"] = value; }
		}

		protected void Page_Load(object sender, EventArgs e) {
			this.RegisterAsyncTask(
				new PageAsyncTask(
					async ct => {
						var twitter = new TwitterConsumer();
						if (twitter.ConsumerKey != null) {
							this.MultiView1.ActiveViewIndex = 1;

							if (!IsPostBack) {
								// Is Twitter calling back with authorization?
								var accessTokenResponse = await twitter.ProcessUserAuthorizationAsync(this.Request.Url);
								if (accessTokenResponse != null) {
									this.AccessToken = accessTokenResponse.AccessToken;
								} else {
									// If we don't yet have access, immediately request it.
									Uri redirectUri = await twitter.RequestUserAuthorizationAsync(MessagingUtilities.GetPublicFacingUrl());
									this.Response.Redirect(redirectUri.AbsoluteUri);
								}
							}
						}
					}));
		}

		protected void downloadUpdates_Click(object sender, EventArgs e) {
			this.RegisterAsyncTask(
				new PageAsyncTask(
					async ct => {
						var twitter = new TwitterConsumer();
						var statusesJson = await twitter.GetUpdatesAsync(this.AccessToken);

						StringBuilder tableBuilder = new StringBuilder();
						tableBuilder.Append("<table><tr><td>Name</td><td>Update</td></tr>");

						foreach (dynamic update in statusesJson) {
							if (!update.user.@protected.Value) {
								tableBuilder.AppendFormat(
									"<tr><td>{0}</td><td>{1}</td></tr>",
									HttpUtility.HtmlEncode(update.user.screen_name),
									HttpUtility.HtmlEncode(update.text));
							}
						}

						tableBuilder.Append("</table>");
						this.resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() });
					}));
		}

		protected void uploadProfilePhotoButton_Click(object sender, EventArgs e) {
			this.RegisterAsyncTask(
				new PageAsyncTask(
					async ct => {
						if (this.profilePhoto.PostedFile.ContentType == null) {
							this.photoUploadedLabel.Visible = true;
							this.photoUploadedLabel.Text = "Select a file first.";
							return;
						}

						var twitter = new TwitterConsumer();
						XDocument imageResult =
							await
							twitter.UpdateProfileImageAsync(
								this.AccessToken, this.profilePhoto.PostedFile.InputStream, this.profilePhoto.PostedFile.ContentType);
						this.photoUploadedLabel.Visible = true;
					}));
		}
	}
}