Search code examples
.netoauthtumblrdotnetopenauth

Custom Tumblr OAuthClient with DotNetOpenAuth


Does anyone know any custom OAuthClient for Tumblr? I was going through the Tumblr api documentation and I ended up having almost no clue on how to create one to use with DotNetOpenAuth.


Solution

  • something like this may work:

    Web Config

    <add key="TumblrKey" value="x" />
    <add key="TumblrSecret" value="x" />
    

    Token Store (from/based on the samples project):

        public class InMemoryTokenManager : IConsumerTokenManager
    {
        private readonly Dictionary<string, string> tokensAndSecrets = new Dictionary<string, string>();
    
        public InMemoryTokenManager(string consumerKey, string consumerSecret)
        {
            if(string.IsNullOrEmpty(consumerKey))
            {
                throw new ArgumentNullException("consumerKey");
            }
    
            ConsumerKey = consumerKey;
            ConsumerSecret = consumerSecret;
        }
    
        public string ConsumerKey { get; private set; }
    
        public string ConsumerSecret { get; private set; }
    
        #region ITokenManager Members
    
        public string GetTokenSecret(string token)
        {
            return tokensAndSecrets[token];
        }
    
        public void StoreNewRequestToken(UnauthorizedTokenRequest request, ITokenSecretContainingMessage response)
        {
            tokensAndSecrets[response.Token] = response.TokenSecret;
        }
    
    
        public void ExpireRequestTokenAndStoreNewAccessToken(string consumerKey, string requestToken, string accessToken, string accessTokenSecret)
        {
            tokensAndSecrets.Remove(requestToken);
            tokensAndSecrets[accessToken] = accessTokenSecret;
        }
    
        public TokenType GetTokenType(string token)
        {
            throw new NotImplementedException();
        }
    
        #endregion
    }
    

    Controller

    public class TumblrController : Controller
    {
        private const string AuthorizationUrl = "http://www.tumblr.com/oauth/authorize";
        private const string AccessTokenUrl = "http://www.tumblr.com/oauth/access_token";
        private const string RequestTokenUrl = "http://www.tumblr.com/oauth/request_token";
    
        private static readonly ConcurrentDictionary<string, InMemoryTokenManager> TokenStore = new ConcurrentDictionary<string, InMemoryTokenManager>();
    
        private static readonly WebConsumer TumblrConsumer;
    
        static TumblrController()
        {
            TumblrConsumer = new WebConsumer(TumblrServiceProviderDescription, ShortTermUserSessionTokenManager);
        }
    
        public ActionResult Auth()
        {
            var callBack = new Uri("http://your.com/callback");
            var request = TumblrConsumer.Channel.PrepareResponse(TumblrConsumer.PrepareRequestUserAuthorization(callBack, null, null));
            request.Send();
    
            return new EmptyResult();
        }
    
        public ActionResult Callback()
        {
            var auth = TumblrConsumer.ProcessUserAuthorization();
            // do something with auth
    
            return new EmptyResult();
        }
    
        private static ServiceProviderDescription TumblrServiceProviderDescription
        {
            get
            {
                var tumblrDescription = new ServiceProviderDescription
                {
                    UserAuthorizationEndpoint = new MessageReceivingEndpoint(AuthorizationUrl, HttpDeliveryMethods.GetRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
                    AccessTokenEndpoint = new MessageReceivingEndpoint(AccessTokenUrl, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
                    RequestTokenEndpoint = new MessageReceivingEndpoint(RequestTokenUrl, HttpDeliveryMethods.PostRequest | HttpDeliveryMethods.AuthorizationHeaderRequest),
                    TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }
                };
    
                return tumblrDescription;   
            }
        }
    
        private static InMemoryTokenManager ShortTermUserSessionTokenManager
        {
            get
            {
                InMemoryTokenManager tokenManager;
                TokenStore.TryGetValue("Tumblr", out tokenManager);
    
                if (tokenManager == null)
                {
                    tokenManager = new InMemoryTokenManager(ConfigurationManager.AppSettings["TumblrKey"], ConfigurationManager.AppSettings["TumblrSecret"]);
                    TokenStore.TryAdd("Tumblr", tokenManager);
                }
    
                return tokenManager;
            }
        }
    }