Skip to content
Browse files

API fix since access token consists of two parts

  • Loading branch information...
1 parent 2f28582 commit 017f27556a52f7215a3960104ce6bec8be30c06b @flq flq committed Nov 6, 2010
View
17 Membus.WpfTwitterClient/Frame/Config/CheapUserSettings.cs
@@ -8,17 +8,24 @@ public class CheapUserSettings : IUserSettings
public CheapUserSettings()
{
if (!File.Exists("user.txt"))
- File.CreateText("user.txt").Dispose();
+ return;
using (var r = File.OpenText("user.txt"))
- AccessToken = r.ReadToEnd();
+ {
+ AccessToken = new TwitterAccessToken(r.ReadLine(), r.ReadLine());
+ }
}
- public string AccessToken { get; private set; }
+ public bool IsAccessTokenAvailable
+ {
+ get { return AccessToken != null; }
+ }
+
+ public TwitterAccessToken AccessToken { get; private set; }
- public void StoreAccessToken(string token)
+ public void StoreAccessToken(TwitterAccessToken token)
{
AccessToken = token;
- File.WriteAllText("user.txt", AccessToken);
+ File.WriteAllText("user.txt", token.Token + Environment.NewLine + token.Secret);
}
}
}
View
7 Membus.WpfTwitterClient/Frame/Config/IUserSettings.cs
@@ -1,8 +1,11 @@
+using Membus.WpfTwitterClient.Frame.Config;
+
namespace Membus.WpfTwitterClient.Frame
{
public interface IUserSettings
{
- string AccessToken { get; }
- void StoreAccessToken(string token);
+ bool IsAccessTokenAvailable { get; }
+ TwitterAccessToken AccessToken { get; }
+ void StoreAccessToken(TwitterAccessToken token);
}
}
View
24 Membus.WpfTwitterClient/Frame/Config/TwitterAccessToken.cs
@@ -0,0 +1,24 @@
+namespace Membus.WpfTwitterClient.Frame.Config
+{
+ public class TwitterAccessToken
+ {
+ private readonly string token;
+ private readonly string secret;
+
+ public TwitterAccessToken(string token, string secret)
+ {
+ this.token = token;
+ this.secret = secret;
+ }
+
+ public string Secret
+ {
+ get { return secret; }
+ }
+
+ public string Token
+ {
+ get { return token; }
+ }
+ }
+}
View
3 Membus.WpfTwitterClient/Frame/Twitter/ITwitterSession.cs
@@ -1,10 +1,11 @@
using System;
+using Membus.WpfTwitterClient.Frame.Config;
namespace Membus.WpfTwitterClient.Frame.Twitter
{
public interface ITwitterSession
{
void GetAuthorizationUrl(Action<Uri> authorizationUriAvailable);
- void GetAccessToken(string verifyCode, Action<string> accessTokenAvailable);
+ void GetAccessToken(string verifyCode, Action<TwitterAccessToken> accessTokenAvailable);
}
}
View
55 Membus.WpfTwitterClient/Frame/Twitter/TwitterFacade.cs
@@ -1,5 +1,7 @@
using System;
+using System.Collections.Generic;
using System.Threading.Tasks;
+using Membus.WpfTwitterClient.Frame.Config;
using Twitterizer;
namespace Membus.WpfTwitterClient.Frame.Twitter
@@ -9,7 +11,8 @@ public class TwitterSession : ITwitterSession
private readonly TwitterKeys keys;
private readonly Func<IUserSettings> userSettingsLoader;
private string requestToken;
- private string accessToken;
+
+ readonly TaskFactory taskFactory = new TaskFactory();
public TwitterSession(TwitterKeys keys, Func<IUserSettings> userSettingsLoader)
{
@@ -19,27 +22,47 @@ public TwitterSession(TwitterKeys keys, Func<IUserSettings> userSettingsLoader)
public void GetAuthorizationUrl(Action<Uri> authorizationUriAvailable)
{
- var t = new Task(
- () =>
- {
- var response = OAuthUtility.GetRequestToken(keys.ConsumerKey, keys.ConsumerSecret, "oob");
- requestToken = response.Token;
- var uri = OAuthUtility.BuildAuthorizationUri(requestToken);
- authorizationUriAvailable(uri);
- });
- t.Start();
+ taskFactory.StartNew(() =>
+ {
+ var response = OAuthUtility.GetRequestToken(keys.ConsumerKey,
+ keys.ConsumerSecret, "oob");
+ requestToken = response.Token;
+ var uri = OAuthUtility.BuildAuthorizationUri(requestToken);
+ authorizationUriAvailable(uri);
+ });
}
- public void GetAccessToken(string verifyCode, Action<string> accessTokenAvailable)
+ public void GetAccessToken(string verifyCode, Action<TwitterAccessToken> accessTokenAvailable)
{
- var t = new Task(
- () =>
+ taskFactory.StartNew(() =>
{
var response = OAuthUtility.GetAccessToken(keys.ConsumerKey, keys.ConsumerSecret, requestToken, verifyCode);
- accessToken = response.Token;
- accessTokenAvailable(accessToken);
+ accessTokenAvailable(new TwitterAccessToken(response.Token, response.TokenSecret));
});
- t.Start();
+ }
+
+ public void LoadPublicTimeline(Action<ICollection<TwitterStatus>> action)
+ {
+ taskFactory.StartNew(() =>
+ {
+ var statuses = TwitterTimeline.PublicTimeline(getOAuthToken());
+ action(statuses);
+ });
+ }
+
+ private OAuthTokens getOAuthToken()
+ {
+ var userSettings = userSettingsLoader();
+ if (!userSettings.IsAccessTokenAvailable)
+ throw new InvalidOperationException("By some reason no access token is available, this is an invalid state of the application.");
+ var token = userSettings.AccessToken;
+ return new OAuthTokens
+ {
+ AccessToken = token.Token,
+ AccessTokenSecret = token.Secret,
+ ConsumerKey = keys.ConsumerKey,
+ ConsumerSecret = keys.ConsumerSecret
+ };
}
}
}
View
1 Membus.WpfTwitterClient/Membus.WpfTwitterClient.csproj
@@ -100,6 +100,7 @@
<Compile Include="Frame\Config\BaseConfigurationSection.cs" />
<Compile Include="Frame\Config\ConfigReader.cs" />
<Compile Include="Frame\Config\IUserSettings.cs" />
+ <Compile Include="Frame\Config\TwitterAccessToken.cs" />
<Compile Include="Frame\Config\TwitterKeysConfiguration.cs" />
<Compile Include="Frame\ActionOnDispatcher.cs" />
<Compile Include="Frame\Twitter\ITwitterSession.cs" />
View
7 Membus.WpfTwitterClient/Properties/Settings.cs
@@ -1,13 +1,14 @@
using System;
using Membus.WpfTwitterClient.Frame;
+using Membus.WpfTwitterClient.Frame.Config;
namespace Membus.WpfTwitterClient.Properties
{
- internal partial class Settings : IUserSettings
+ internal partial class Settings //: IUserSettings
{
- public void StoreAccessToken(string token)
+ public void StoreAccessToken(TwitterAccessToken token)
{
- AccessToken = token;
+ //AccessToken = token;
Save();
}
}
View
6 Membus.WpfTwitterClient/Startup/StartupHandler.cs
@@ -19,9 +19,9 @@ public StartupHandler(IUserSettings settings, IBus bus)
protected override void push(RequestToStartup message)
{
- var screenToActivate = string.IsNullOrEmpty(settings.AccessToken)
- ? typeof (GetAccessTokenViewModel)
- : typeof (MainTimelineViewModel);
+ var screenToActivate = settings.IsAccessTokenAvailable
+ ? typeof (MainTimelineViewModel)
+ : typeof (GetAccessTokenViewModel);
bus.Publish(new RequestToActivateMainScreen(screenToActivate));
}
}

0 comments on commit 017f275

Please sign in to comment.
Something went wrong with that request. Please try again.