view Agendas/trunk/src/Agendas.Twitter/ITwitterAdapter.cs @ 210:5cc7bc973ffc

Ticket 175: Que al confirmar un evento, muestre una pantalla sugiriendo el servicio de scheduler.
author juanjose.montesdeocaarbos
date Fri, 26 Aug 2011 07:26:29 -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;

		}
	}
}