diff options
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj | 1 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/YammerConsumer.cs | 61 | ||||
-rw-r--r-- | samples/OAuthConsumer/OAuthConsumer.csproj | 8 | ||||
-rw-r--r-- | samples/OAuthConsumer/Web.config | 3 | ||||
-rw-r--r-- | samples/OAuthConsumer/Yammer.aspx | 48 | ||||
-rw-r--r-- | samples/OAuthConsumer/Yammer.aspx.cs | 76 | ||||
-rw-r--r-- | samples/OAuthConsumer/Yammer.aspx.designer.cs | 123 |
7 files changed, 320 insertions, 0 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj index 6739bf9..8a580e6 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj +++ b/samples/DotNetOpenAuth.ApplicationBlock/DotNetOpenAuth.ApplicationBlock.csproj @@ -93,6 +93,7 @@ <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="TwitterConsumer.cs" /> <Compile Include="Util.cs" /> + <Compile Include="YammerConsumer.cs" /> <Compile Include="YubikeyRelyingParty.cs" /> </ItemGroup> <ItemGroup> diff --git a/samples/DotNetOpenAuth.ApplicationBlock/YammerConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/YammerConsumer.cs new file mode 100644 index 0000000..1e4aeef --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/YammerConsumer.cs @@ -0,0 +1,61 @@ +//----------------------------------------------------------------------- +// <copyright file="YammerConsumer.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.ApplicationBlock { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net; + using System.Text; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + using DotNetOpenAuth.OAuth.ChannelElements; + using DotNetOpenAuth.OAuth.Messages; + + public static class YammerConsumer { + /// <summary> + /// The Consumer to use for accessing Google data APIs. + /// </summary> + public static readonly ServiceProviderDescription ServiceDescription = new ServiceProviderDescription { + RequestTokenEndpoint = new MessageReceivingEndpoint("https://www.yammer.com/oauth/request_token", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest), + UserAuthorizationEndpoint = new MessageReceivingEndpoint("https://www.yammer.com/oauth/authorize", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.GetRequest), + AccessTokenEndpoint = new MessageReceivingEndpoint("https://www.yammer.com/oauth/access_token", HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest), + TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new PlaintextSigningBindingElement() }, + ProtocolVersion = ProtocolVersion.V10, + }; + + public static DesktopConsumer CreateConsumer(IConsumerTokenManager tokenManager) { + return new DesktopConsumer(ServiceDescription, tokenManager); + } + + public static Uri PrepareRequestAuthorization(DesktopConsumer consumer, out string requestToken) { + if (consumer == null) { + throw new ArgumentNullException("consumer"); + } + + Uri authorizationUrl = consumer.RequestUserAuthorization(null, null, out requestToken); + return authorizationUrl; + } + + public static AuthorizedTokenResponse CompleteAuthorization(DesktopConsumer consumer, string requestToken, string userCode) { + // Because Yammer has a proprietary callback_token parameter, and it's passed + // with the message that specifically bans extra arguments being passed, we have + // to cheat by adding the data to the URL itself here. + var customServiceDescription = new ServiceProviderDescription { + RequestTokenEndpoint = ServiceDescription.RequestTokenEndpoint, + UserAuthorizationEndpoint = ServiceDescription.UserAuthorizationEndpoint, + AccessTokenEndpoint = new MessageReceivingEndpoint(ServiceDescription.AccessTokenEndpoint.Location.AbsoluteUri + "?oauth_verifier=" + Uri.EscapeDataString(userCode), HttpDeliveryMethods.AuthorizationHeaderRequest | HttpDeliveryMethods.PostRequest), + TamperProtectionElements = ServiceDescription.TamperProtectionElements, + ProtocolVersion = ProtocolVersion.V10, + }; + + // To use a custom service description we also must create a new WebConsumer. + var customConsumer = new DesktopConsumer(customServiceDescription, consumer.TokenManager); + var response = customConsumer.ProcessUserAuthorization(requestToken, userCode); + return response; + } + } +} diff --git a/samples/OAuthConsumer/OAuthConsumer.csproj b/samples/OAuthConsumer/OAuthConsumer.csproj index 8092ba2..1cdde47 100644 --- a/samples/OAuthConsumer/OAuthConsumer.csproj +++ b/samples/OAuthConsumer/OAuthConsumer.csproj @@ -62,6 +62,7 @@ <Content Include="GoogleAddressBook.aspx" /> <Content Include="images\Sign-in-with-Twitter-darker.png" /> <Content Include="SampleWcf.aspx" /> + <Content Include="Yammer.aspx" /> <None Include="Service References\SampleServiceProvider\DataApi.disco" /> <None Include="Service References\SampleServiceProvider\configuration91.svcinfo" /> <None Include="Service References\SampleServiceProvider\configuration.svcinfo" /> @@ -130,6 +131,13 @@ <Compile Include="Twitter.aspx.designer.cs"> <DependentUpon>Twitter.aspx</DependentUpon> </Compile> + <Compile Include="Yammer.aspx.cs"> + <DependentUpon>Yammer.aspx</DependentUpon> + <SubType>ASPXCodeBehind</SubType> + </Compile> + <Compile Include="Yammer.aspx.designer.cs"> + <DependentUpon>Yammer.aspx</DependentUpon> + </Compile> </ItemGroup> <ItemGroup> <Folder Include="App_Data\" /> diff --git a/samples/OAuthConsumer/Web.config b/samples/OAuthConsumer/Web.config index 7753874..2d8d817 100644 --- a/samples/OAuthConsumer/Web.config +++ b/samples/OAuthConsumer/Web.config @@ -50,6 +50,9 @@ <!-- Google sign-up: https://www.google.com/accounts/ManageDomains --> <add key="googleConsumerKey" value="anonymous"/> <add key="googleConsumerSecret" value="anonymous"/> + <!-- Yammer sign-up: https://www.yammer.com/client_applications/new --> + <add key="yammerConsumerKey" value=""/> + <add key="yammerConsumerSecret" value=""/> </appSettings> <connectionStrings/> diff --git a/samples/OAuthConsumer/Yammer.aspx b/samples/OAuthConsumer/Yammer.aspx new file mode 100644 index 0000000..90b61b8 --- /dev/null +++ b/samples/OAuthConsumer/Yammer.aspx @@ -0,0 +1,48 @@ +<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" + CodeBehind="Yammer.aspx.cs" Inherits="OAuthConsumer.Yammer" %> + +<asp:Content ID="Content2" ContentPlaceHolderID="Body" runat="Server"> + <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0"> + <asp:View ID="ClientRegistrationRequiredView" runat="server"> + <h2> + Yammer setup</h2> + <p> + A Yammer client app must be registered. + </p> + <ol> + <li><a target="_blank" href="https://www.yammer.com/client_applications/new">Visit Yammer + and register a client app</a>. </li> + <li>Modify your web.config file to include your consumer key and consumer secret. + </li> + </ol> + </asp:View> + <asp:View ID="BeginAuthorizationView" runat="server"> + <asp:Label Text="An error occurred in authorization. You may try again." EnableViewState="false" Visible="false" ForeColor="Red" ID="authorizationErrorLabel" runat="server" /> + <asp:Button Text="Obtain authorization now" runat="server" ID="obtainAuthorizationButton" + OnClick="obtainAuthorizationButton_Click" /> + </asp:View> + <asp:View ID="CompleteAuthorizationView" runat="server"> + After you have authorized Yammer to share your information, please enter the code + Yammer gives you here: + <asp:TextBox runat="server" ID="yammerUserCode" EnableViewState="false" /> + <asp:RequiredFieldValidator ErrorMessage="*" ControlToValidate="yammerUserCode" runat="server" /> + <asp:Button Text="Finish" runat="server" ID="finishAuthorizationButton" OnClick="finishAuthorizationButton_Click" /> + </asp:View> + <asp:View ID="AuthorizationCompleteView" runat="server"> + <h2> + Updates + </h2> + <p>The access token we have obtained is: + <asp:Label ID="accessTokenLabel" runat="server" /> + </p> + <p> + Ok, Yammer has authorized us to download your messages. Click 'Get messages' + to download the latest few messages to this sample. Notice how we never asked you + for your Yammer username or password. + </p> + <asp:Button ID="getYammerMessagesButton" runat="server" OnClick="getYammerMessages_Click" + Text="Get address book" /> + <asp:PlaceHolder ID="resultsPlaceholder" runat="server" /> + </asp:View> + </asp:MultiView> +</asp:Content> diff --git a/samples/OAuthConsumer/Yammer.aspx.cs b/samples/OAuthConsumer/Yammer.aspx.cs new file mode 100644 index 0000000..0c5b59e --- /dev/null +++ b/samples/OAuthConsumer/Yammer.aspx.cs @@ -0,0 +1,76 @@ +namespace OAuthConsumer { + using System; + using System.Collections.Generic; + using System.Configuration; + using System.Linq; + using System.Web; + using System.Web.UI; + using System.Web.UI.WebControls; + using DotNetOpenAuth.ApplicationBlock; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OAuth; + + public partial class Yammer : System.Web.UI.Page { + private string RequestToken { + get { return (string)ViewState["YammerRequestToken"]; } + set { ViewState["YammerRequestToken"] = value; } + } + + private string AccessToken { + get { return (string)Session["YammerAccessToken"]; } + set { Session["YammerAccessToken"] = value; } + } + + private InMemoryTokenManager TokenManager { + get { + var tokenManager = (InMemoryTokenManager)Application["YammerTokenManager"]; + if (tokenManager == null) { + string consumerKey = ConfigurationManager.AppSettings["YammerConsumerKey"]; + string consumerSecret = ConfigurationManager.AppSettings["YammerConsumerSecret"]; + if (!string.IsNullOrEmpty(consumerKey)) { + tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret); + Application["YammerTokenManager"] = tokenManager; + } + } + + return tokenManager; + } + } + + protected void Page_Load(object sender, EventArgs e) { + if (this.TokenManager != null) { + this.MultiView1.SetActiveView(BeginAuthorizationView); + } + } + + protected void getYammerMessages_Click(object sender, EventArgs e) { + var yammer = new WebConsumer(YammerConsumer.ServiceDescription, this.TokenManager); + } + + protected void obtainAuthorizationButton_Click(object sender, EventArgs e) { + var yammer = YammerConsumer.CreateConsumer(this.TokenManager); + string requestToken; + Uri popupWindowLocation = YammerConsumer.PrepareRequestAuthorization(yammer, out requestToken); + this.RequestToken = requestToken; + string javascript = "window.open('" + popupWindowLocation.AbsoluteUri + "');"; + this.Page.ClientScript.RegisterStartupScript(GetType(), "YammerPopup", javascript, true); + MultiView1.SetActiveView(CompleteAuthorizationView); + } + + protected void finishAuthorizationButton_Click(object sender, EventArgs e) { + if (!Page.IsValid) { + return; + } + + var yammer = YammerConsumer.CreateConsumer(this.TokenManager); + var authorizationResponse = YammerConsumer.CompleteAuthorization(yammer, this.RequestToken, yammerUserCode.Text); + if (authorizationResponse != null) { + this.accessTokenLabel.Text = HttpUtility.HtmlEncode(authorizationResponse.AccessToken); + this.MultiView1.SetActiveView(this.AuthorizationCompleteView); + } else { + this.MultiView1.SetActiveView(this.BeginAuthorizationView); + this.authorizationErrorLabel.Visible = true; + } + } + } +}
\ No newline at end of file diff --git a/samples/OAuthConsumer/Yammer.aspx.designer.cs b/samples/OAuthConsumer/Yammer.aspx.designer.cs new file mode 100644 index 0000000..57f6db5 --- /dev/null +++ b/samples/OAuthConsumer/Yammer.aspx.designer.cs @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// <auto-generated> +// This code was generated by a tool. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// </auto-generated> +//------------------------------------------------------------------------------ + +namespace OAuthConsumer { + + + public partial class Yammer { + + /// <summary> + /// MultiView1 control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.MultiView MultiView1; + + /// <summary> + /// ClientRegistrationRequiredView control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.View ClientRegistrationRequiredView; + + /// <summary> + /// BeginAuthorizationView control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.View BeginAuthorizationView; + + /// <summary> + /// authorizationErrorLabel control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Label authorizationErrorLabel; + + /// <summary> + /// obtainAuthorizationButton control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Button obtainAuthorizationButton; + + /// <summary> + /// CompleteAuthorizationView control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.View CompleteAuthorizationView; + + /// <summary> + /// yammerUserCode control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.TextBox yammerUserCode; + + /// <summary> + /// finishAuthorizationButton control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Button finishAuthorizationButton; + + /// <summary> + /// AuthorizationCompleteView control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.View AuthorizationCompleteView; + + /// <summary> + /// accessTokenLabel control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Label accessTokenLabel; + + /// <summary> + /// getYammerMessagesButton control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.Button getYammerMessagesButton; + + /// <summary> + /// resultsPlaceholder control. + /// </summary> + /// <remarks> + /// Auto-generated field. + /// To modify move field declaration from designer file to code-behind file. + /// </remarks> + protected global::System.Web.UI.WebControls.PlaceHolder resultsPlaceholder; + } +} |