From 63bbedd3a004eab47fd706a758ba248bb395e26f Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:18:58 -0400 Subject: [PATCH 01/15] Creating new initialization method Passing username and password to authorize method --- TwitterXAuth.h | 19 ++++++++++++++++++- TwitterXAuth.m | 20 +++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/TwitterXAuth.h b/TwitterXAuth.h index 9c0b336..27e9c17 100644 --- a/TwitterXAuth.h +++ b/TwitterXAuth.h @@ -54,6 +54,23 @@ typedef enum { @property (nonatomic,copy) NSString * token; //oauth_token @property (nonatomic,copy) NSString * tokenSecret; //oauth_token_secret @property (nonatomic,assign) id delegate; -- (void) authorize; + +/* + * Use this to create your TwitterXAuth object. Then call authorizeWithUsername:andPassword: + * when the user has entered his/her credentials + */ +- (id) initWithConsumerKey:(NSString *)key secret:(NSString *)secret andDelegate:(id)del; + +/* + * Send an authentication request to Twitter + * @param username the user's twitter handle + * @param password the user's twitter password + */ +- (void) authorizeWithUsername:(NSString *)username andPassword:(NSString *)password; + +/* + * Send a status update to twitter on behalf of authenticated user + * @param status the tweet text + */ - (void) tweet:(NSString *)status; @end diff --git a/TwitterXAuth.m b/TwitterXAuth.m index 1244db1..4756a75 100644 --- a/TwitterXAuth.m +++ b/TwitterXAuth.m @@ -55,11 +55,19 @@ - (id) init if ((self = [super init])) { state = TwitterXAuthStateDefault; data = [[NSMutableData alloc] init]; - self.tokenSecret = @""; } return self; } +- (id) initWithConsumerKey:(NSString *)key secret:(NSString *)secret andDelegate:(id)del +{ + self = [self init]; + self.consumerKey = key; + self.consumerSecret = secret; + self.delegate = del; + return self; +} + - (void) dealloc { [data release]; @@ -144,7 +152,8 @@ - (NSString *) baseString - (NSString *) signature { - NSString * secret = [NSString stringWithFormat:@"%@&%@", self.consumerSecret, self.tokenSecret]; + if(!self.tokenSecret) self.tokenSecret = @""; + NSString *secret = [NSString stringWithFormat:@"%@&%@", self.consumerSecret, self.tokenSecret]; NSData * secretData = [secret dataUsingEncoding:NSUTF8StringEncoding]; NSData * baseData = [self.baseString dataUsingEncoding:NSUTF8StringEncoding]; @@ -173,8 +182,10 @@ - (NSString *) authorizationHeader return [NSString stringWithFormat:@"OAuth %@", [keysAndValues componentsJoinedByString:@", "]]; } -- (void) authorize +- (void) authorizeWithUsername:(NSString *)_username andPassword:(NSString *)_password { + self.username = _username; + self.password = _password; //send POST to https://api.twitter.com/oauth/access_token with parameters: x_auth_username, x_auth_password, x_auth_mode [self resetTimestamp]; @@ -231,6 +242,7 @@ - (void) tweet:(NSString *)status - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { + NSLog(@"error %@", [error localizedDescription]); if (state == TwitterXAuthStateAuthorize && delegate && [delegate respondsToSelector:@selector(twitterXAuthAuthorizationDidFail:)]) [delegate twitterXAuthAuthorizationDidFail:self]; if (state == TwitterXAuthStateTweet && delegate && [delegate respondsToSelector:@selector(twitterXAuthTweetDidFail:)]) @@ -267,6 +279,8 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection NSString * response = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]; NSArray * parameters = [response componentsSeparatedByString:@"&"]; + NSLog(@"response parameters %@", parameters); + NSMutableDictionary * dictionary = [NSMutableDictionary dictionary]; for (NSString * parameter in parameters) { NSArray * keyAndValue = [parameter componentsSeparatedByString:@"="]; From b84a4ef01ea5246476cc69458af3e5cdc46758c4 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:20:15 -0400 Subject: [PATCH 02/15] disabling default init method --- TwitterXAuth.m | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/TwitterXAuth.m b/TwitterXAuth.m index 4756a75..0ad0f21 100644 --- a/TwitterXAuth.m +++ b/TwitterXAuth.m @@ -52,16 +52,15 @@ @implementation TwitterXAuth - (id) init { - if ((self = [super init])) { - state = TwitterXAuthStateDefault; - data = [[NSMutableData alloc] init]; - } - return self; + [NSException raise:@"Initialization Error" format:@"you must initialize with initWithConsumerKey:secret:andDelegate:"]; + return nil; } - (id) initWithConsumerKey:(NSString *)key secret:(NSString *)secret andDelegate:(id)del { - self = [self init]; + self = [super init]; + state = TwitterXAuthStateDefault; + data = [[NSMutableData alloc] init]; self.consumerKey = key; self.consumerSecret = secret; self.delegate = del; From 5fdf45202800ad9f500c763ee7afba98a2e1985d Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:24:11 -0400 Subject: [PATCH 03/15] README updates --- README | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/README b/README index e24088f..a85d24a 100644 --- a/README +++ b/README @@ -12,8 +12,46 @@ See twitter-xauth.m for example usage. Fill in values for CONSUMER_KEY, CONSUMER_SECRET, TWITTER_USERNAME, and TWITTER_PASSWORD. Compiles on Mac OS X using gnu make. -This code is possible thanks to documentation and code examples found -on the following websites: + +Usage +----- + +# Initialize TwitterXAuth + +```objective-c +TwitterXAuth *twitter = [[TwitterXAuth alloc] initWithConsumerKey:@"your key" secret:@"your secret" andDelegate:self]; +``` +Make sure you implement the delegate methods you need: + +```objective-c +# pragma mark - TwitterXAuth + +- (void) twitterXAuthAuthorizationDidFail:(TwitterXAuth *)twitterXAuth +{ + NSLog(@"auth failed"); +} + +- (void) twitterXAuthDidAuthorize:(TwitterXAuth *)twitterXAuth +{ + NSLog(@"auth succeeded"); +} + +- (void) twitterXAuthTweetDidFail:(TwitterXAuth *)twitterXAuth +{ +} + +- (void) twitterXAuthDidTweet:(TwitterXAuth *)twitterXAuth +{ + +} +``` +# Authenticate user + +```objective-c +[twitter authorizeWithUsername:loginObject.userName andPassword:loginObject.password]; +``` + +See more documentation about XAuth using the links below: http://weblog.bluedonkey.org/?p=959 http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html From 686c9568bb18ccaa7ab2f597da698a10f56de7e0 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:24:41 -0400 Subject: [PATCH 04/15] moving readme to markdown --- README => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README => README.md (100%) diff --git a/README b/README.md similarity index 100% rename from README rename to README.md From 0e192ca62eb74de958df225e2dcd8661a8e2193d Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:27:21 -0400 Subject: [PATCH 05/15] README updates --- README.md | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a85d24a..9fc0df5 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ twitter-xauth -------------- +============ This is an extremely simple twitter client for Mac OS X and iOS, which uses xauth for authentication. The only other feature provided are @@ -14,7 +14,7 @@ TWITTER_PASSWORD. Compiles on Mac OS X using gnu make. Usage ------ +===== # Initialize TwitterXAuth @@ -35,21 +35,31 @@ Make sure you implement the delegate methods you need: { NSLog(@"auth succeeded"); } +``` +# Authenticate user + +```objective-c +[twitter authorizeWithUsername:loginObject.userName andPassword:loginObject.password]; +``` + +# Tweet on the user's behalf + +```objective-c +[twitter tweet:@"Hi from Twitter-XAuth"]; +``` +Make sure you implement the tweet delegate methods if you need: +```objective-c - (void) twitterXAuthTweetDidFail:(TwitterXAuth *)twitterXAuth { + NSLog(@"tweet failed"; } - (void) twitterXAuthDidTweet:(TwitterXAuth *)twitterXAuth { - + NSLog(@"tweet succeeded"); } ``` -# Authenticate user - -```objective-c -[twitter authorizeWithUsername:loginObject.userName andPassword:loginObject.password]; -``` See more documentation about XAuth using the links below: From 6b426cf4e3d23f3db14f6e11b7b96ac690a039c8 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:27:50 -0400 Subject: [PATCH 06/15] README updates --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9fc0df5..500787c 100644 --- a/README.md +++ b/README.md @@ -63,13 +63,13 @@ Make sure you implement the tweet delegate methods if you need: See more documentation about XAuth using the links below: -http://weblog.bluedonkey.org/?p=959 -http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html -http://www.getsharekit.com/ -http://www.gigliwood.com/weblog/Cocoa/Q__When_is_an_conne.html -http://dev.twitter.com/pages/xauth -http://dev.twitter.com/doc/post/oauth/access_token -http://dev.twitter.com/pages/auth#auth-request +* http://weblog.bluedonkey.org/?p=959 +* http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html +* http://www.getsharekit.com/ +* http://www.gigliwood.com/weblog/Cocoa/Q__When_is_an_conne.html +* http://dev.twitter.com/pages/xauth +* http://dev.twitter.com/doc/post/oauth/access_token +* http://dev.twitter.com/pages/auth#auth-request Special thanks to Matt Gallagher of cocoawithlove.com for providing an Objective-C implementation of Base64 encoding for Mac and iPhone. From f1f8cb120c1321ec91aacfd3a23933cefe7beb5d Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 12:28:55 -0400 Subject: [PATCH 07/15] whitespace cleanup --- TwitterXAuth.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TwitterXAuth.m b/TwitterXAuth.m index 0ad0f21..fef9899 100644 --- a/TwitterXAuth.m +++ b/TwitterXAuth.m @@ -241,7 +241,7 @@ - (void) tweet:(NSString *)status - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { - NSLog(@"error %@", [error localizedDescription]); + NSLog(@"error %@", [error localizedDescription]); if (state == TwitterXAuthStateAuthorize && delegate && [delegate respondsToSelector:@selector(twitterXAuthAuthorizationDidFail:)]) [delegate twitterXAuthAuthorizationDidFail:self]; if (state == TwitterXAuthStateTweet && delegate && [delegate respondsToSelector:@selector(twitterXAuthTweetDidFail:)]) From 6436fd14e7cbae871e862de3a7ad857c6e932005 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:26:23 -0400 Subject: [PATCH 08/15] installation instructions --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 500787c..7b8914a 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,20 @@ CONSUMER_KEY, CONSUMER_SECRET, TWITTER_USERNAME, and TWITTER_PASSWORD. Compiles on Mac OS X using gnu make. +Installation +=========== + +You'll need to copy the following files into your project: +* TwitterXAuth.h +* TwitterXAuth.m +* NSData+Base64.h (assuming you're not already using this) +* NSData+Base64.m (assuming you're not already using this) + +Then make sure you import in whatever controllers you need: + +```objective-c +#import "TwitterXAuth.h" +``` Usage ===== From 4bea8d7ee451966440f7da6b72bc9baac52958a2 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:30:10 -0400 Subject: [PATCH 09/15] adding sso info --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 7b8914a..31a5940 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,13 @@ Make sure you implement the tweet delegate methods if you need: NSLog(@"tweet succeeded"); } ``` +# Single Sign On +Should you need to log a user in to your backend with Twitter, you'll need to grab the user's token and tokenSecret from the TwitterXAuth object: + +```objective-c + NSLog(@"twitter token: '%@'", twitter.token); + NSLog(@"twitter token secret: '%@'", twitter.tokenSecret); +``` See more documentation about XAuth using the links below: From 8295ffa3494d2880ffc6215b379ebb4d1b3306e9 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:38:02 -0400 Subject: [PATCH 10/15] added callback for token and secret --- TwitterXAuth.h | 6 ++++++ TwitterXAuth.m | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/TwitterXAuth.h b/TwitterXAuth.h index 27e9c17..694100e 100644 --- a/TwitterXAuth.h +++ b/TwitterXAuth.h @@ -23,6 +23,12 @@ - (void) twitterXAuthDidAuthorize:(TwitterXAuth *)twitterXAuth; - (void) twitterXAuthTweetDidFail:(TwitterXAuth *)twitterXAuth; - (void) twitterXAuthDidTweet:(TwitterXAuth *)twitterXAuth; + +/* + * Implement this method if you want to get the token and secret immediately + * after authorization + */ +- (void) twitterXAuthDidGetToken:(NSString *)token andTokenSecret:(NSString *)secret; @end typedef enum { diff --git a/TwitterXAuth.m b/TwitterXAuth.m index fef9899..4289ef2 100644 --- a/TwitterXAuth.m +++ b/TwitterXAuth.m @@ -300,6 +300,10 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection if (delegate && [delegate respondsToSelector:@selector(twitterXAuthDidTweet:)]) [delegate twitterXAuthDidTweet:self]; } + if(self.token && ![self.tokenSecret isEqualToString:@""]){ + if([self.delegate respondsToSelector:@selector(twitterXAuthDidGetToken:andTokenSecret:)]) + [self.delegate twitterXAuthDidGetToken:self.token andTokenSecret:self.tokenSecret]; + } } @end From 42bda9ec132698dbf478840b0dd314e453403be9 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:41:43 -0400 Subject: [PATCH 11/15] renamed token/secret callback and added documentation --- README.md | 12 +++++++++--- TwitterXAuth.h | 2 +- TwitterXAuth.m | 4 ++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 31a5940..547b630 100644 --- a/README.md +++ b/README.md @@ -75,12 +75,18 @@ Make sure you implement the tweet delegate methods if you need: } ``` # Single Sign On -Should you need to log a user in to your backend with Twitter, you'll need to grab the user's token and tokenSecret from the TwitterXAuth object: +Should you need to log a user in to your backend with Twitter, you'll need to grab the user's token and tokenSecret from the TwitterXAuth object. The easiest way to do this is to implement the twitterXAuthDidRetrieveToken:andSecret method: ```objective-c - NSLog(@"twitter token: '%@'", twitter.token); - NSLog(@"twitter token secret: '%@'", twitter.tokenSecret); +- (void) twitterXAuthDidRetrieveToken:(NSString *)token andTokenSecret:(NSString *)secret +{ + // Do something useful with the token and secret +} ``` +It's important to note that you probably want to choose to implement either twitterXAuthDidAuthorize OR twitterXAuthDidRetrieveToken:andSecret because they both will be called upon authorization if they are implemented. + +More +==== See more documentation about XAuth using the links below: diff --git a/TwitterXAuth.h b/TwitterXAuth.h index 694100e..69b834c 100644 --- a/TwitterXAuth.h +++ b/TwitterXAuth.h @@ -28,7 +28,7 @@ * Implement this method if you want to get the token and secret immediately * after authorization */ -- (void) twitterXAuthDidGetToken:(NSString *)token andTokenSecret:(NSString *)secret; +- (void) twitterXAuthDidRetrieveToken:(NSString *)token andTokenSecret:(NSString *)secret; @end typedef enum { diff --git a/TwitterXAuth.m b/TwitterXAuth.m index 4289ef2..58c69d6 100644 --- a/TwitterXAuth.m +++ b/TwitterXAuth.m @@ -301,8 +301,8 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection [delegate twitterXAuthDidTweet:self]; } if(self.token && ![self.tokenSecret isEqualToString:@""]){ - if([self.delegate respondsToSelector:@selector(twitterXAuthDidGetToken:andTokenSecret:)]) - [self.delegate twitterXAuthDidGetToken:self.token andTokenSecret:self.tokenSecret]; + if([self.delegate respondsToSelector:@selector(twitterXAuthDidRetrieveToken:andTokenSecret:)]) + [self.delegate twitterXAuthDidRetrieveToken:self.token andTokenSecret:self.tokenSecret]; } } From 2e4e2aa691f50750d79455b0ee1cd2abc72c678e Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:43:37 -0400 Subject: [PATCH 12/15] instructions for getting XAuth access --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 547b630..b5acd03 100644 --- a/README.md +++ b/README.md @@ -13,6 +13,12 @@ CONSUMER_KEY, CONSUMER_SECRET, TWITTER_USERNAME, and TWITTER_PASSWORD. Compiles on Mac OS X using gnu make. +Requisites +========== + +Your Twitter app must have XAuth enabled. To do so, follow the instructions on this page: +https://dev.twitter.com/docs/oauth/xauth + Installation =========== From be9347e02fdcc15a2089554946dc14488c039e68 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:45:53 -0400 Subject: [PATCH 13/15] instructions for getting XAuth access --- README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5acd03..e2f7e8c 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,15 @@ TWITTER_PASSWORD. Compiles on Mac OS X using gnu make. Requisites ========== -Your Twitter app must have XAuth enabled. To do so, follow the instructions on this page: +Your Twitter app must have XAuth enabled. To do so, follow the +instructions on this page: https://dev.twitter.com/docs/oauth/xauth +Don't be intimidated. As long as you provide all the necessary info +to Twitter they will likely grant you access. XAuth will save you a +lot of time compared to implementing MGTwitterEngine and represents a +good solution for integrating iOS with Twitter until iOS 5.0 arrives. + Installation =========== From 08e6860bcc07127d53b9786cc3fd4282a3408289 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:46:45 -0400 Subject: [PATCH 14/15] README formatting --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e2f7e8c..5d04f0e 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ Then make sure you import in whatever controllers you need: Usage ===== -# Initialize TwitterXAuth +####Initialize TwitterXAuth ```objective-c TwitterXAuth *twitter = [[TwitterXAuth alloc] initWithConsumerKey:@"your key" secret:@"your secret" andDelegate:self]; @@ -62,13 +62,13 @@ Make sure you implement the delegate methods you need: NSLog(@"auth succeeded"); } ``` -# Authenticate user +#### Authenticate user ```objective-c [twitter authorizeWithUsername:loginObject.userName andPassword:loginObject.password]; ``` -# Tweet on the user's behalf +#### Tweet on the user's behalf ```objective-c [twitter tweet:@"Hi from Twitter-XAuth"]; @@ -86,7 +86,7 @@ Make sure you implement the tweet delegate methods if you need: NSLog(@"tweet succeeded"); } ``` -# Single Sign On +#### Single Sign On Should you need to log a user in to your backend with Twitter, you'll need to grab the user's token and tokenSecret from the TwitterXAuth object. The easiest way to do this is to implement the twitterXAuthDidRetrieveToken:andSecret method: ```objective-c From 4e3fcf0dadca467e921e7def4d37f413a8e35453 Mon Sep 17 00:00:00 2001 From: Josh Stephenson Date: Sun, 4 Sep 2011 17:47:34 -0400 Subject: [PATCH 15/15] README formatting --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 5d04f0e..cd6bc2e 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ Make sure you implement the tweet delegate methods if you need: } ``` #### Single Sign On -Should you need to log a user in to your backend with Twitter, you'll need to grab the user's token and tokenSecret from the TwitterXAuth object. The easiest way to do this is to implement the twitterXAuthDidRetrieveToken:andSecret method: +Should you need to log a user in to your backend with Twitter, you'll need to grab the user's token and tokenSecret from the TwitterXAuth object. The easiest way to do this is to implement the `twitterXAuthDidRetrieveToken:andSecret` method: ```objective-c - (void) twitterXAuthDidRetrieveToken:(NSString *)token andTokenSecret:(NSString *)secret @@ -95,7 +95,7 @@ Should you need to log a user in to your backend with Twitter, you'll need to gr // Do something useful with the token and secret } ``` -It's important to note that you probably want to choose to implement either twitterXAuthDidAuthorize OR twitterXAuthDidRetrieveToken:andSecret because they both will be called upon authorization if they are implemented. +It's important to note that you probably want to choose to implement either `twitterXAuthDidAuthorize` OR `twitterXAuthDidRetrieveToken:andSecret` because they both will be called upon authorization if they are implemented. More ====