using System; using Hammock; using MahApps.RESTBase; using MahApps.Twitter; using MahApps.Twitter.Models; using MahTweets.Mobile.Core; namespace MahTweets.Mobile.TwitterPlugin.ViewModels { public class TwitterSetupViewModel : MahViewModelBase { private const string ConsumerKey = "NRvCz7KI8MRodk2E28Dmg"; private const string ConsumerSecret = "4Pm8OlyWRUmuWmG25t7yhbtoUW4A9V0CA4XO8tvg"; private readonly TwitterClient _twitterClient; private Credentials _tokens; private Uri _address; public TwitterSetupViewModel() { _twitterClient = new TwitterClient(ConsumerKey, ConsumerSecret, "http://www.mahtweets.com/mobile_oauth.html"); } public Uri Address { get { return _address; } set { _address = value; RaisePropertyChanged(() => Address); } } public void VerifyCredentials(RestRequest request, RestResponse response, object obj) { if (obj is User && !String.IsNullOrEmpty(((User)obj).Name)) { var u = (User)obj; Twitter.OnSuccess(new Credential { AccountName = u.ScreenName, Username = _tokens.OAuthToken, Password = _tokens.OAuthTokenSecret, Protocol = "twitter", AccountId = u.Id.ToString() }); } else Twitter.OnFailure("Twitter setup cancelled or failed"); } public void Login(string text, string password) { _twitterClient.BeginXAuthAuthenticate(text, password, (request, response, credentials) => { if (credentials == null) Twitter.OnFailure( "Twitter setup cancelled or failed"); else { _tokens = credentials; _twitterClient.SetOAuthToken(credentials); _twitterClient.Account.BeginVerifyCredentials(VerifyCredentials); } }); } } }