view Agendas/trunk/src/Agendas.Twitter/ITwitterAdapter.cs @ 298:9bc60d166c8a

Se corrigieron los tests por el cambio de Patrocinador, para que no persista el logo en disco. Se comentó el código de PatrocinadorApiController, que no se utiliza.
author juanjose.montesdeocaarbos
date Sun, 19 Feb 2012 16:00:38 -0300
parents 734d3f0853bf
children
line wrap: on
line source

using System;
using AltNetHispano.Agendas.Configurations;
using Twitterizer;

namespace AltNetHispano.Agendas.Twitter
{
	public interface ITwitterAdapter
	{
		Uri BuildAuthorizationUri(string consumerKey, string consumerSecret, string callbackAddress);
		string GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string verifier);
		bool Update(string status, out string message);
	}

	public class TwitterAdapter : ITwitterAdapter
	{
		private static TwitterConfigurationElement _config;

		public TwitterAdapter()
		{
			_config = AgendasConfigurationManager.Publicadores.Twitter;
		}

		private static string ConsumerKey
		{
			get { return _config.SendTwitter.ConsumerKey; }
		}

		private static string ConsumerSecret
		{
			get { return _config.SendTwitter.ConsumerSecret; }
		}


		private static string AccessToken
		{
			get { return _config.SendTwitter.AccessToken; }
		}


		private static string AccessTokenSecret
		{
			get { return _config.SendTwitter.AccessTokenSecret; }
		}

		public Uri BuildAuthorizationUri(string consumerKey, string consumerSecret, string callbackAddress)
		{
			// Obtain a request token
			OAuthTokenResponse requestToken = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, callbackAddress);
			
			// Direct or instruct the user to the following address:
			Uri authorizationUri = OAuthUtility.BuildAuthorizationUri(requestToken.Token);

			return authorizationUri;
		}

		public string GetAccessToken(string consumerKey, string consumerSecret, string requestToken, string verifier)
		{
			OAuthTokenResponse accessToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, requestToken, verifier);

			if (accessToken != null)
			{
				var tokens = new OAuthTokens
				{
					AccessToken = accessToken.Token,
					AccessTokenSecret = accessToken.TokenSecret,
					ConsumerKey = consumerKey,
					ConsumerSecret = consumerSecret
				};

				TwitterResponse<TwitterUser> showUserResponse = TwitterUser.Show(tokens, accessToken.UserId);
				if (showUserResponse.Result == RequestResult.Success)
					return showUserResponse.Content;
				return showUserResponse.ErrorMessage;
			}
			
			return string.Empty;

		}

		public bool Update(string status, out string message)
		{
			var tokens = new OAuthTokens
							{
								ConsumerKey = ConsumerKey,
								ConsumerSecret = ConsumerSecret,
								AccessToken = AccessToken,
								AccessTokenSecret = AccessTokenSecret
							};

			var response = TwitterStatus.Update(tokens, status);

			message = response.ErrorMessage;
			return response.Result == RequestResult.Success;

		}
	}
}