summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj1
-rw-r--r--samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs77
-rw-r--r--samples/OAuthConsumer/Default.aspx8
-rw-r--r--samples/OAuthConsumer/Twitter.aspx29
-rw-r--r--samples/OAuthConsumer/Twitter.aspx.cs86
-rw-r--r--samples/OAuthConsumer/Web.config7
6 files changed, 204 insertions, 4 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj
index 590b96b..626dbaa 100644
--- a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj
+++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj
@@ -57,6 +57,7 @@
<ItemGroup>
<Compile Include="GoogleConsumer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
+ <Compile Include="TwitterConsumer.cs" />
<Compile Include="Util.cs" />
</ItemGroup>
<ItemGroup>
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs
new file mode 100644
index 0000000..cd90365
--- /dev/null
+++ b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs
@@ -0,0 +1,77 @@
+//-----------------------------------------------------------------------
+// <copyright file="TwitterConsumer.cs" company="Andrew Arnott">
+// Copyright (c) Andrew Arnott. All rights reserved.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace DotNetOpenAuth.ApplicationBlock {
+ using System;
+ using System.Collections.Generic;
+ using System.IO;
+ using System.Xml;
+ using System.Xml.Linq;
+ using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.OAuth;
+ using DotNetOpenAuth.OAuth.ChannelElements;
+
+ /// <summary>
+ /// A consumer capable of communicating with Twitter.
+ /// </summary>
+ public static class TwitterConsumer {
+ /// <summary>
+ /// The description of Twitter's OAuth protocol URIs.
+ /// </summary>
+ private static readonly ServiceProviderDescription TwitterDescription = new ServiceProviderDescription {
+ RequestTokenEndpoint = new MessageReceivingEndpoint("http://twitter.com/oauth/request_token", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
+ UserAuthorizationEndpoint = new MessageReceivingEndpoint("http://twitter.com/oauth/authorize", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
+ AccessTokenEndpoint = new MessageReceivingEndpoint("http://twitter.com/oauth/access_token", HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
+ TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() },
+ };
+
+ /// <summary>
+ /// The URI to get a user's favorites.
+ /// </summary>
+ private static readonly MessageReceivingEndpoint GetFavoritesEndpoint = new MessageReceivingEndpoint("http://twitter.com/favorites.xml", HttpDeliveryMethods.GetRequest);
+
+ /// <summary>
+ /// The URI to get the data on the user's home page.
+ /// </summary>
+ private static readonly MessageReceivingEndpoint GetFriendTimelineStatusEndpoint = new MessageReceivingEndpoint("http://twitter.com/statuses/friends_timeline.xml", HttpDeliveryMethods.GetRequest);
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="WebConsumer"/> class that is
+ /// prepared to communicate with Twitter.
+ /// </summary>
+ /// <param name="tokenManager">The token manager.</param>
+ /// <param name="consumerKey">The consumer key.</param>
+ /// <returns>The newly instantiated <see cref="WebConsumer"/>.</returns>
+ public static WebConsumer CreateWebConsumer(ITokenManager tokenManager, string consumerKey) {
+ return new WebConsumer(TwitterDescription, tokenManager) {
+ ConsumerKey = consumerKey,
+ };
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="DesktopConsumer"/> class that is
+ /// prepared to communicate with Twitter.
+ /// </summary>
+ /// <param name="tokenManager">The token manager.</param>
+ /// <param name="consumerKey">The consumer key.</param>
+ /// <returns>The newly instantiated <see cref="DesktopConsumer"/>.</returns>
+ public static DesktopConsumer CreateDesktopConsumer(ITokenManager tokenManager, string consumerKey) {
+ return new DesktopConsumer(TwitterDescription, tokenManager) {
+ ConsumerKey = consumerKey,
+ };
+ }
+
+ public static XDocument GetUpdates(ConsumerBase twitter, string accessToken) {
+ IncomingWebResponse response = twitter.PrepareAuthorizedRequestAndSend(GetFriendTimelineStatusEndpoint, accessToken);
+ return XDocument.Load(XmlReader.Create(response.GetResponseReader()));
+ }
+
+ public static XDocument GetFavorites(ConsumerBase twitter, string accessToken) {
+ IncomingWebResponse response = twitter.PrepareAuthorizedRequestAndSend(GetFavoritesEndpoint, accessToken);
+ return XDocument.Load(XmlReader.Create(response.GetResponseReader()));
+ }
+ }
+}
diff --git a/samples/OAuthConsumer/Default.aspx b/samples/OAuthConsumer/Default.aspx
index 20e0f94..aa4ef79 100644
--- a/samples/OAuthConsumer/Default.aspx
+++ b/samples/OAuthConsumer/Default.aspx
@@ -1,11 +1,13 @@
-<%@ Page Title="DotNetOpenAuth Consumer samples" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" %>
+<%@ Page Title="DotNetOpenAuth Consumer samples" Language="C#" MasterPageFile="~/MasterPage.master"
+ AutoEventWireup="true" %>
<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server">
- <p>OAuth allows this web site to access your private data with your authorization,
- but without you having to give up your password. </p>
+ <p>OAuth allows this web site to access your private data with your authorization, but
+ without you having to give up your password. </p>
<p>Select a demo:</p>
<ul>
<li><a href="GoogleAddressBook.aspx">Download your Gmail address book</a></li>
+ <li><a href="Twitter.aspx">Get your Twitter updates</a></li>
<li><a href="SampleWcf.aspx">Interop with Service Provider sample using WCF w/ OAuth</a></li>
</ul>
</asp:Content>
diff --git a/samples/OAuthConsumer/Twitter.aspx b/samples/OAuthConsumer/Twitter.aspx
new file mode 100644
index 0000000..2a36b84
--- /dev/null
+++ b/samples/OAuthConsumer/Twitter.aspx
@@ -0,0 +1,29 @@
+<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
+ CodeFile="Twitter.aspx.cs" Inherits="Twitter" %>
+
+<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
+</asp:Content>
+<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server">
+ <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
+ <asp:View runat="server">
+ <p>May we download your Twitter feeds? Click Authorize to let Twitter know you're ok
+ with this. </p>
+ <asp:Button ID="authorizeButton" runat="server" Text="Authorize" OnClick="authorizeButton_Click" />
+ </asp:View>
+ <asp:View runat="server">
+ <h2>Updates</h2>
+ <p>Ok, Twitter has authorized us to download your feeds. Click 'Get updates' to download
+ updates to this sample. Notice how we never asked you for your Twitter username
+ or password. </p>
+ <asp:Button ID="downloadUpdates" runat="server" Text="Get updates" OnClick="downloadUpdates_Click" />
+ <asp:PlaceHolder runat="server" ID="resultsPlaceholder" />
+ </asp:View>
+ <asp:View runat="server">
+ <h2>Twitter setup</h2>
+ <p>A Twitter client app must be endorsed by a Twitter user. <a target="_blank" href="http://twitter.com/oauth_clients">
+ Visit Twitter and create a client app</a>. Then come back here modify your web.config
+ file to include your consumer key and consumer secret. Then this page will light
+ up. </p>
+ </asp:View>
+ </asp:MultiView>
+</asp:Content>
diff --git a/samples/OAuthConsumer/Twitter.aspx.cs b/samples/OAuthConsumer/Twitter.aspx.cs
new file mode 100644
index 0000000..354fe69
--- /dev/null
+++ b/samples/OAuthConsumer/Twitter.aspx.cs
@@ -0,0 +1,86 @@
+using System;
+using System.Collections.Generic;
+using System.Configuration;
+using System.Linq;
+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;
+
+public partial class Twitter : System.Web.UI.Page {
+ private string AccessToken {
+ get { return (string)ViewState["AccessToken"]; }
+ set { ViewState["AccessToken"] = value; }
+ }
+
+ private InMemoryTokenManager TokenManager {
+ get {
+ var tokenManager = (InMemoryTokenManager)Session["TokenManager"];
+ if (tokenManager == null) {
+ string consumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"];
+ string consumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"];
+ if (!string.IsNullOrEmpty(consumerKey)) {
+ tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
+ Session["TokenManager"] = tokenManager;
+ }
+ }
+
+ return tokenManager;
+ }
+ }
+
+ protected void Page_Load(object sender, EventArgs e) {
+ if (!IsPostBack) {
+ if (TokenManager != null) {
+ InMemoryTokenManager tokenManager = (InMemoryTokenManager)Session["TokenManager"];
+ var twitter = TwitterConsumer.CreateWebConsumer(tokenManager, tokenManager.ConsumerKey);
+
+ var accessTokenResponse = twitter.ProcessUserAuthorization();
+ if (accessTokenResponse != null) {
+ // User has approved access
+ MultiView1.ActiveViewIndex = 1;
+
+ this.AccessToken = accessTokenResponse.AccessToken;
+ }
+ } else {
+ MultiView1.ActiveViewIndex = 2;
+ }
+ }
+ }
+
+ protected void authorizeButton_Click(object sender, EventArgs e) {
+ if (!Page.IsValid) {
+ return;
+ }
+
+ var twitter = TwitterConsumer.CreateWebConsumer(this.TokenManager, this.TokenManager.ConsumerKey);
+ twitter.Channel.Send(twitter.PrepareRequestUserAuthorization());
+ }
+
+ protected void downloadUpdates_Click(object sender, EventArgs e) {
+ var twitter = TwitterConsumer.CreateWebConsumer(this.TokenManager, this.TokenManager.ConsumerKey);
+ XPathDocument updates = new XPathDocument(TwitterConsumer.GetUpdates(twitter, AccessToken).CreateReader());
+ XPathNavigator nav = updates.CreateNavigator();
+ var parsedUpdates = from status in nav.Select("/statuses/status").OfType<XPathNavigator>()
+ where !status.SelectSingleNode("user/protected").ValueAsBoolean
+ select new {
+ User = status.SelectSingleNode("user/name").InnerXml,
+ Status = status.SelectSingleNode("text").InnerXml,
+ };
+
+ StringBuilder tableBuilder = new StringBuilder();
+ tableBuilder.Append("<table><tr><td>Name</td><td>Update</td></tr>");
+
+ foreach (var update in parsedUpdates) {
+ tableBuilder.AppendFormat(
+ "<tr><td>{0}</td><td>{1}</td></tr>",
+ HttpUtility.HtmlEncode(update.User),
+ HttpUtility.HtmlEncode(update.Status));
+ }
+ tableBuilder.Append("</table>");
+ resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() });
+ }
+}
diff --git a/samples/OAuthConsumer/Web.config b/samples/OAuthConsumer/Web.config
index 7d7f6aa..4e58136 100644
--- a/samples/OAuthConsumer/Web.config
+++ b/samples/OAuthConsumer/Web.config
@@ -14,7 +14,12 @@
</sectionGroup>
</sectionGroup>
</configSections>
- <appSettings/>
+ <appSettings>
+ <!-- Fill in your various consumer keys and secrets here to make the sample work. -->
+ <!-- You must get these values by signing up with each individual service provider. -->
+ <add key="twitterConsumerKey" value="" />
+ <add key="twitterConsumerSecret" value="" />
+ </appSettings>
<connectionStrings/>
<system.web>
<!--