view Agendas/trunk/src/Agendas.Twitter/ITwitterAdapter.cs @ 119:8fa58a79656a

Historico
author Nelo@Kenia.neluz.int
date Wed, 22 Jun 2011 09:49:07 -0300
parents 0bca45e1e664
children 734d3f0853bf
line wrap: on
line source

using System;
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);
		TwitterResponse<TwitterStatus> UpdateStatus(OAuthTokens tokens, string status);
	}

	public class TwitterAdapter : ITwitterAdapter
	{
		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 TwitterResponse<TwitterStatus> UpdateStatus(OAuthTokens tokens, string status)
		{
			return TwitterStatus.Update(tokens, status);
		}
	}
}