diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-03-21 14:10:41 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-03-21 14:10:41 -0700 |
commit | ec8e3b27155297b790cadcfdebbe24fa2cbfa639 (patch) | |
tree | bd710d4964741fddad097b66e7b5e0b6eeca8b1b /samples/DotNetOpenAuth.ApplicationBlock | |
parent | 4197cf4144223397b3c1aa03eb136b34820c3684 (diff) | |
download | DotNetOpenAuth-ec8e3b27155297b790cadcfdebbe24fa2cbfa639.zip DotNetOpenAuth-ec8e3b27155297b790cadcfdebbe24fa2cbfa639.tar.gz DotNetOpenAuth-ec8e3b27155297b790cadcfdebbe24fa2cbfa639.tar.bz2 |
Added Blogger posting OAuth sample.
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock')
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs | 55 | ||||
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/Util.cs | 49 |
2 files changed, 103 insertions, 1 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs index 2206fff..c6f2b89 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/GoogleConsumer.cs @@ -7,7 +7,13 @@ namespace DotNetOpenAuth.ApplicationBlock { using System; using System.Collections.Generic; + using System.Diagnostics; + using System.IO; using System.Linq; + using System.Net; + using System.Text; + using System.Text.RegularExpressions; + using System.Xml; using System.Xml.Linq; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth; @@ -33,6 +39,7 @@ namespace DotNetOpenAuth.ApplicationBlock { private static readonly Dictionary<Applications, string> DataScopeUris = new Dictionary<Applications, string> { { Applications.Contacts, "http://www.google.com/m8/feeds/" }, { Applications.Calendar, "http://www.google.com/calendar/feeds/" }, + { Applications.Blogger, "http://www.blogger.com/feeds" }, }; /// <summary> @@ -54,6 +61,11 @@ namespace DotNetOpenAuth.ApplicationBlock { /// Appointments in Google Calendar. /// </summary> Calendar = 0x2, + + /// <summary> + /// Blog post authoring. + /// </summary> + Blogger = 0x4, } /// <summary> @@ -134,6 +146,49 @@ namespace DotNetOpenAuth.ApplicationBlock { return result; } + public static void PostBlogEntry(ConsumerBase consumer, string accessToken, string blogUrl, string title, XElement body) { + string feedUrl; + var getBlogHome = WebRequest.Create(blogUrl); + using (var blogHomeResponse = getBlogHome.GetResponse()) { + using (StreamReader sr = new StreamReader(blogHomeResponse.GetResponseStream())) { + string homePageHtml = sr.ReadToEnd(); + Match m = Regex.Match(homePageHtml, @"http://www.blogger.com/feeds/\d+/posts/default"); + Debug.Assert(m.Success, "Posting operation failed."); + feedUrl = m.Value; + } + } + const string Atom = "http://www.w3.org/2005/Atom"; + XElement entry = new XElement( + XName.Get("entry", Atom), + new XElement(XName.Get("title", Atom), new XAttribute("type", "text"), title), + new XElement(XName.Get("content", Atom), new XAttribute("type", "xhtml"), body), + new XElement(XName.Get("category", Atom), new XAttribute("scheme", "http://www.blogger.com/atom/ns#"), new XAttribute("term", "oauthdemo"))); + + MemoryStream ms = new MemoryStream(); + XmlWriterSettings xws = new XmlWriterSettings() { + Encoding = Encoding.UTF8, + }; + XmlWriter xw = XmlWriter.Create(ms, xws); + entry.WriteTo(xw); + xw.Flush(); + + WebRequest request = consumer.PrepareAuthorizedRequest(new MessageReceivingEndpoint(feedUrl, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest), accessToken); + request.ContentType = "application/atom+xml"; + request.Method = "POST"; + request.ContentLength = ms.Length; + ms.Seek(0, SeekOrigin.Begin); + using (Stream requestStream = request.GetRequestStream()) { + ms.CopyTo(requestStream); + } + using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { + if (response.StatusCode == HttpStatusCode.Created) { + // Success + } else { + // Error! + } + } + } + /// <summary> /// Gets the scope URI in Google's format. /// </summary> diff --git a/samples/DotNetOpenAuth.ApplicationBlock/Util.cs b/samples/DotNetOpenAuth.ApplicationBlock/Util.cs index 5001e2e..ea7da97 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/Util.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/Util.cs @@ -1,9 +1,10 @@ namespace DotNetOpenAuth.ApplicationBlock { using System; using System.Collections.Generic; + using System.IO; using DotNetOpenAuth.Messaging; - internal class Util { + internal static class Util { /// <summary> /// Enumerates through the individual set bits in a flag enum. /// </summary> @@ -25,5 +26,51 @@ Uri callback = MessagingUtilities.GetRequestUrlFromContext().StripQueryArgumentsWithPrefix("oauth_"); return callback; } + + /// <summary> + /// Copies the contents of one stream to another. + /// </summary> + /// <param name="copyFrom">The stream to copy from, at the position where copying should begin.</param> + /// <param name="copyTo">The stream to copy to, at the position where bytes should be written.</param> + /// <returns>The total number of bytes copied.</returns> + /// <remarks> + /// Copying begins at the streams' current positions. + /// The positions are NOT reset after copying is complete. + /// </remarks> + internal static int CopyTo(this Stream copyFrom, Stream copyTo) { + return CopyTo(copyFrom, copyTo, int.MaxValue); + } + + /// <summary> + /// Copies the contents of one stream to another. + /// </summary> + /// <param name="copyFrom">The stream to copy from, at the position where copying should begin.</param> + /// <param name="copyTo">The stream to copy to, at the position where bytes should be written.</param> + /// <param name="maximumBytesToCopy">The maximum bytes to copy.</param> + /// <returns>The total number of bytes copied.</returns> + /// <remarks> + /// Copying begins at the streams' current positions. + /// The positions are NOT reset after copying is complete. + /// </remarks> + internal static int CopyTo(this Stream copyFrom, Stream copyTo, int maximumBytesToCopy) { + if (copyFrom == null) { + throw new ArgumentNullException("copyFrom"); + } + if (copyTo == null) { + throw new ArgumentNullException("copyTo"); + } + + byte[] buffer = new byte[1024]; + int readBytes; + int totalCopiedBytes = 0; + while ((readBytes = copyFrom.Read(buffer, 0, Math.Min(1024, maximumBytesToCopy))) > 0) { + int writeBytes = Math.Min(maximumBytesToCopy, readBytes); + copyTo.Write(buffer, 0, writeBytes); + totalCopiedBytes += writeBytes; + maximumBytesToCopy -= writeBytes; + } + + return totalCopiedBytes; + } } } |