Instantly share code, notes, and snippets.
Created
February 20, 2011 13:08
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save takeshik/835953 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.IO; | |
namespace LinqToTwitter | |
{ | |
public class MetaTweetAuthorizer | |
: OAuthAuthorizer, | |
ITwitterAuthorizer | |
{ | |
private static readonly Byte[] _entropy = Convert.FromBase64String( | |
@"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA | |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" | |
); | |
private readonly FileInfo _tokenFile; | |
public String AccountId | |
{ | |
get; | |
protected set; | |
} | |
public Func<Uri, String> GetPin | |
{ | |
get; | |
set; | |
} | |
public MetaTweetAuthorizer(FileInfo tokenFile) | |
{ | |
this._tokenFile = tokenFile; | |
this.Credentials = new InMemoryCredentials() | |
{ | |
ConsumerKey = "xxxxxxxxxxxxxxxxxxxxxx", | |
ConsumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", | |
}; | |
this.OAuthTwitter = new OAuthTwitter() | |
{ | |
OAuthConsumerKey = this.Credentials.ConsumerKey, | |
OAuthConsumerSecret = this.Credentials.ConsumerSecret, | |
}; | |
} | |
public void Authorize() | |
{ | |
if (!this.IsAuthorized) | |
{ | |
this.Load(); | |
if (!this.IsAuthorized) | |
{ | |
String screenName; | |
String userID; | |
if (this.GetPin == null) | |
{ | |
throw new InvalidOperationException("GetPin must have a handler before calling Authorize."); | |
} | |
String link = OAuthTwitter.AuthorizationLinkGet(this.OAuthRequestTokenUrl, this.OAuthAuthorizeUrl, "oob", false); | |
String verifier = this.GetPin(new Uri(link)); | |
String oAuthToken = new Uri(link).Query.TrimStart('?') | |
.Split('&') | |
.Select(s => s.Split('=')) | |
.Where(_ => _[0] == "oauth_token") | |
.Select(_ => _[1]) | |
.SingleOrDefault(); | |
this.OAuthTwitter.AccessTokenGet(oAuthToken, verifier, this.OAuthAccessTokenUrl, "", out screenName, out userID); | |
this.ScreenName = screenName; | |
this.UserId = userID; | |
this.Credentials.OAuthToken = this.OAuthTwitter.OAuthToken; | |
this.Credentials.AccessToken = this.OAuthTwitter.OAuthTokenSecret; | |
using (SHA1Cng sha1 = new SHA1Cng()) | |
{ | |
this.AccountId = String.Join("", sha1.ComputeHash(Encoding.BigEndianUnicode.GetBytes("!Id=" + userID + "@com.twitter")) | |
.Select(b => b.ToString("x2"))); | |
} | |
this.Save(); | |
} | |
} | |
} | |
public void Load() | |
{ | |
if (this._tokenFile.Exists) | |
{ | |
try | |
{ | |
String[] data = Encoding.UTF32.GetString(ProtectedData.Unprotect( | |
File.ReadAllBytes(this._tokenFile.FullName), | |
_entropy, | |
DataProtectionScope.LocalMachine | |
)) | |
.Split( | |
new String[] { "\0\0\0\0" }, | |
5, | |
StringSplitOptions.None | |
); | |
this.Credentials.OAuthToken = data[0]; | |
this.Credentials.AccessToken = data[1]; | |
this.UserId = data[2]; | |
this.ScreenName = data[3]; | |
this.AccountId = data[4]; | |
} | |
catch (Exception) | |
{ | |
this.Credentials.OAuthToken = null; | |
this.Credentials.AccessToken = null; | |
this.UserId = null; | |
this.ScreenName = null; | |
this.AccountId = null; | |
} | |
finally | |
{ | |
this.OAuthTwitter.OAuthToken = this.Credentials.OAuthToken; | |
this.OAuthTwitter.OAuthTokenSecret = this.Credentials.AccessToken; | |
} | |
} | |
} | |
public void Save() | |
{ | |
if (this.IsAuthorized) | |
{ | |
File.WriteAllBytes(this._tokenFile.FullName, ProtectedData.Protect( | |
Encoding.UTF32.GetBytes(String.Join( | |
"\0\0\0\0", | |
new String[] | |
{ | |
this.Credentials.OAuthToken, | |
this.Credentials.AccessToken, | |
this.UserId, | |
this.ScreenName, | |
this.AccountId, | |
} | |
)), | |
_entropy, | |
DataProtectionScope.LocalMachine | |
)); | |
} | |
} | |
public void Load(String credentialsString) | |
{ | |
String[] credentials = credentialsString.Split(','); | |
this.Credentials.OAuthToken = credentials[0]; | |
this.Credentials.AccessToken = credentials[1]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment