diff options
Diffstat (limited to 'samples')
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs | 40 | ||||
-rw-r--r-- | samples/OAuthConsumer/Twitter.aspx | 16 | ||||
-rw-r--r-- | samples/OAuthConsumer/Twitter.aspx.cs | 16 |
3 files changed, 69 insertions, 3 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs index 2a98ffe..ecb7d6c 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs @@ -8,6 +8,7 @@ namespace DotNetOpenAuth.ApplicationBlock { using System; using System.Collections.Generic; using System.IO; + using System.Net; using System.Xml; using System.Xml.Linq; using DotNetOpenAuth.Messaging; @@ -38,6 +39,18 @@ namespace DotNetOpenAuth.ApplicationBlock { /// </summary> private static readonly MessageReceivingEndpoint GetFriendTimelineStatusEndpoint = new MessageReceivingEndpoint("http://twitter.com/statuses/friends_timeline.xml", HttpDeliveryMethods.GetRequest); + private static readonly MessageReceivingEndpoint UpdateProfileBackgroundImageEndpoint = new MessageReceivingEndpoint("http://twitter.com/account/update_profile_background_image.xml", HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest); + + private static readonly MessageReceivingEndpoint UpdateProfileImageEndpoint = new MessageReceivingEndpoint("http://twitter.com/account/update_profile_image.xml", HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest); + + /// <summary> + /// Initializes static members of the <see cref="TwitterConsumer"/> class. + /// </summary> + static TwitterConsumer() { + // Twitter can't handle the Expect 100 Continue HTTP header. + ServicePointManager.FindServicePoint(GetFavoritesEndpoint.Location).Expect100Continue = false; + } + public static XDocument GetUpdates(ConsumerBase twitter, string accessToken) { IncomingWebResponse response = twitter.PrepareAuthorizedRequestAndSend(GetFriendTimelineStatusEndpoint, accessToken); return XDocument.Load(XmlReader.Create(response.GetResponseReader())); @@ -47,5 +60,32 @@ namespace DotNetOpenAuth.ApplicationBlock { IncomingWebResponse response = twitter.PrepareAuthorizedRequestAndSend(GetFavoritesEndpoint, accessToken); return XDocument.Load(XmlReader.Create(response.GetResponseReader())); } + + public static XDocument UpdateProfileBackgroundImage(ConsumerBase twitter, string accessToken, string image, bool tile) { + HttpWebRequest request = twitter.PrepareAuthorizedRequest(UpdateProfileBackgroundImageEndpoint, accessToken); + request.ServicePoint.Expect100Continue = false; + var parts = new[] { + MultipartPostPart.CreateFormFilePart("image", image, "image/" + Path.GetExtension(image).Substring(1).ToLowerInvariant()), + MultipartPostPart.CreateFormPart("tile", tile.ToString().ToLowerInvariant()), + }; + IncomingWebResponse response = request.PostMultipart(twitter.Channel.WebRequestHandler, parts); + string responseString = response.GetResponseReader().ReadToEnd(); + return XDocument.Parse(responseString); + } + + public static XDocument UpdateProfileImage(ConsumerBase twitter, string accessToken, string pathToImage) { + string contentType = "image/" + Path.GetExtension(pathToImage).Substring(1).ToLowerInvariant(); + return UpdateProfileImage(twitter, accessToken, File.OpenRead(pathToImage), contentType); + } + + public static XDocument UpdateProfileImage(ConsumerBase twitter, string accessToken, Stream image, string contentType) { + HttpWebRequest request = twitter.PrepareAuthorizedRequest(UpdateProfileImageEndpoint, accessToken); + var parts = new[] { + MultipartPostPart.CreateFormFilePart("image", "twitterPhoto", contentType, image), + }; + IncomingWebResponse response = request.PostMultipart(twitter.Channel.WebRequestHandler, parts); + string responseString = response.GetResponseReader().ReadToEnd(); + return XDocument.Parse(responseString); + } } } diff --git a/samples/OAuthConsumer/Twitter.aspx b/samples/OAuthConsumer/Twitter.aspx index a659533..30ce2a0 100644 --- a/samples/OAuthConsumer/Twitter.aspx +++ b/samples/OAuthConsumer/Twitter.aspx @@ -16,9 +16,19 @@ </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> + <p>Ok, Twitter has authorized us to download your feeds. Notice how we never asked + you for your Twitter username or password. </p> + <p> + Upload a new profile photo: + <asp:FileUpload ID="profilePhoto" runat="server" /> + <asp:Button ID="uploadProfilePhotoButton" runat="server" + onclick="uploadProfilePhotoButton_Click" Text="Upload photo" /> + <asp:Label ID="photoUploadedLabel" runat="server" EnableViewState="False" + Text="Done!" Visible="False"></asp:Label> + </p> + <p> + Click 'Get updates' to download updates to this sample. + </p> <asp:Button ID="downloadUpdates" runat="server" Text="Get updates" OnClick="downloadUpdates_Click" /> <asp:PlaceHolder runat="server" ID="resultsPlaceholder" /> </asp:View> diff --git a/samples/OAuthConsumer/Twitter.aspx.cs b/samples/OAuthConsumer/Twitter.aspx.cs index 9b9eced..f309396 100644 --- a/samples/OAuthConsumer/Twitter.aspx.cs +++ b/samples/OAuthConsumer/Twitter.aspx.cs @@ -75,4 +75,20 @@ public partial class Twitter : System.Web.UI.Page { tableBuilder.Append("</table>"); resultsPlaceholder.Controls.Add(new Literal { Text = tableBuilder.ToString() }); } + + protected void uploadProfilePhotoButton_Click(object sender, EventArgs e) { + if (profilePhoto.PostedFile.ContentType == null) { + photoUploadedLabel.Visible = true; + photoUploadedLabel.Text = "Select a file first."; + return; + } + + var twitter = new WebConsumer(TwitterConsumer.ServiceDescription, this.TokenManager); + XDocument imageResult = TwitterConsumer.UpdateProfileImage( + twitter, + this.AccessToken, + profilePhoto.PostedFile.InputStream, + profilePhoto.PostedFile.ContentType); + photoUploadedLabel.Visible = true; + } } |