Posting

Posting with Tumblr# is very easy. The TumblrClient class offers a single method called CreatePostAsync: this method takes a PostData instance that encapsulates all the information necessary to create a post. The PostData class exposes static methods to create instances for all the post types (text, photo, etc.).

Creating a text post

To create a text post please refer to the following code:

namespace Sample
{
    using DontPanic.TumblrSharp;
    using DontPanic.TumblrSharp.Client;
    using DontPanic.TumblrSharp.OAuth;
    using System.Threading.Tasks;
    
    public class ApiTest
    {
        private const string consumerKey = "<your consumer key here>";
        private const string consumerSecret = "<your consumer secret here>";

        //!! replace with the access token you got from the OAuth flow
        private Token accessToken = null; 

        public Task PostTextAsync()
        {
            //instantiate the client factory
            var factory = new TumblrClientFactory();

            //create the client; TumblrClient implements IDisposable (to dispose
            //of the internal HttpClient)
            using (var client = factory.Create<TumblrClient>(consumerKey, consumerSecret, accessToken))
            {
                string body = "This post was created using Tumblr#!";
                string title = "Isn't this cool?";
                string[] tags = new string[] { "coding", ".net" };

                //create the data for a text post: use PostData.CreateText
                var postData = PostData.CreateText(body, title, tags);

                //create the post: pass the name of the blog where to post to
                return client.CreatePostAsync("<user blog name here>", postData);
            }
        }
    }
}


Creating a photo post

Creating photo posts is also very easy, either with a single picture or multiple pictures (photo sets). The following example shows how to post 2 photos to Tumblr, but it can be as easily changed to post just 1 or more than 2:

namespace Sample
{
    using DontPanic.TumblrSharp;
    using DontPanic.TumblrSharp.Client;
    using DontPanic.TumblrSharp.OAuth;
    using System.IO;
    using System.Threading.Tasks;
    
    public class ApiTest
    {
        private const string consumerKey = "<your consumer key here>";
        private const string consumerSecret = "<your consumer secret here>";

        //!! replace with the access token you got from the OAuth flow
        private Token accessToken = null; 

        public Task PostPhotosAsync(Stream photo1, Stream photo2)
        {
            //instantiate the client factory
            var factory = new TumblrClientFactory();

            //create the client; TumblrClient implements IDisposable (to dispose
            //of the internal HttpClient)
            using (var client = factory.Create<TumblrClient>(consumerKey, consumerSecret, accessToken))
            {
                //create the array of photos to post; posting binary files is
                //done by wrapping them in an instance of the BinaryFile class,
                //by passing the inner file either as a Stream or a byte array
                var photos = new BinaryFile[] 
                {
                    new BinaryFile(photo1),
                    new BinaryFile(photo2)
                };

                string caption = "Photos posted by Tumblr#!";

                //create the data for a photo post: use PostData.CreatePhoto
                var postData = PostData.CreatePhoto(photos, caption);

                //create the post: pass the name of the blog where to post to
                return client.CreatePostAsync("<user blog name here>", postData);
            }
        }
    }
}



Last edited Aug 26, 2013 at 3:53 AM by turboronin, version 6