Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

updated with code/notes about OAuth 1.0a changes made on twitter, HTH

  • Loading branch information...
commit 50c1b4097b63144014d0009674616049e628bb81 1 parent ed699fa
Chris Kimpton authored
View
2  .gitignore
@@ -0,0 +1,2 @@
+MGTwitter-OAuth-MyTwitter.ipr
+MGTwitter-OAuth-MyTwitter.iws
View
6 MyTwitter.h
@@ -22,10 +22,14 @@
- (BOOL)isUserAuthorized;
- (MyTwitter*)init;
- (NSString *)sendUpdate:(NSString *)status;
-- (void) askForAccessToken ;
+- (void) askForAccessToken: (NSString*) pincode ;
- (void) askForRequestToken ;
- (NSURLRequest*) authorizeURL;
+- (void) askForPinCode;
+
+
+@property(nonatomic,retain)TwitAccount *currentAccount;
View
78 MyTwitter.m
@@ -39,9 +39,9 @@ - (MyTwitter*)init;
// [self.twitter setUsesSecureConnection:NO];
// Not sure if the url/token is needed now we have oauth. I use the same url as supplied when registering - here - http://twitter.com/oauth_clients/new . For token, I used my app name as I want it to appear, ie same as client.
- [self.twitter setClientName:@"<your client>"
+ [self.twitter setClientName:@"<your client>"
version:@"1"
- URL:@"<your url>"
+ URL:@"<your url>"
token:@"<your token>"];
@@ -79,13 +79,52 @@ - (NSURLRequest*) authorizeURL;
return urlReq;
}
-- (void) askForAccessToken ;
+- (void) askForPinCode;
+{
+ UIAlertView *alert = [[UIAlertView alloc]
+ initWithTitle: @"Twitter Pin Code"
+ message:@"Enter pincode"
+ delegate:self
+ cancelButtonTitle:@"Cancel"
+ otherButtonTitles:@"OK", nil];
+ // this is non std api code... you should write your own popup window to ask user for pincode.
+ [alert addTextFieldWithValue:@"" label:@"Code"];
+
+ // Name field
+ UITextField *tf = [alert textFieldAtIndex:0];
+ tf.clearButtonMode = UITextFieldViewModeWhileEditing;
+ tf.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
+ tf.keyboardAppearance = UIKeyboardAppearanceAlert;
+// tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
+// tf.autocorrectionType = UITextAutocorrectionTypeNo;
+
+ [alert show];
+}
+
+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
+{
+ // 0, is cancel, 1 is ok
+ // if 0, reshow browser, with pin code
+ // if 1, get pincode, if not null, not empty continue else reshow browser
+ NSString* pincode = [[alertView textFieldAtIndex:0] text];
+ if (0 == buttonIndex || nil == pincode || 0 == [pincode length])
+ {
+ // cancel or invalid pin - so do nothing
+ // user must start again
+ } else {
+ // go for access token
+ [self askForAccessToken:pincode];
+ }
+}
+
+- (void) askForAccessToken: (NSString*) pincode ;
{
NSURL *url = [NSURL URLWithString:@"http://twitter.com/oauth/access_token"];
+ self.requestToken.verifier = pincode;
OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
consumer:self.consumer
- token:self.requestToken // we don't have a Token yet
+ token:self.requestToken // we now have a Token!
realm:nil // our service provider doesn't specify a realm
signatureProvider:nil]; // use the default method, HMAC-SHA1
@@ -123,6 +162,11 @@ - (void) askForRequestToken ;
}
- (void) accessTokenTicket:(OAServiceTicket *)ticket didFailWithError:(NSData *)data {
+ if (nil != self.twitDelegate &&
+ [self.twitDelegate respondsToSelector:@selector(twitterAccessfailed)])
+ {
+ [self.twitDelegate twitterAccessfailed];
+ }
}
- (void) accessTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data {
@@ -130,8 +174,12 @@ - (void) accessTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
self.accessToken = [[[OAToken alloc] initWithHTTPResponseBody:responseBody] autorelease];
- [self.twitDelegate setConfig:[self.accessToken key] forKey:kOAuthAccessTokenKey];
- [self.twitDelegate setConfig:[self.accessToken secret] forKey:kOAuthAccessTokenSecret];
+ if (nil != self.twitDelegate &&
+ [self.twitDelegate respondsToSelector:@selector(setConfig:forKey:)])
+ {
+ [self.twitDelegate setConfig:[self.accessToken key] forKey:kOAuthAccessTokenKey];
+ [self.twitDelegate setConfig:[self.accessToken secret] forKey:kOAuthAccessTokenSecret];
+ }
[self.twitter setOaToken:self.accessToken];
}
@@ -150,19 +198,31 @@ - (void)requestTokenTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *
- (void)requestSucceeded:(NSString *)requestIdentifier;
{
NSLog(@"requestSucceeded");
- [self.twitDelegate tweetSucceeded:requestIdentifier];
+ if (nil != self.twitDelegate &&
+ [self.twitDelegate respondsToSelector:@selector(tweetSucceeded:)])
+ {
+ [self.twitDelegate tweetSucceeded:requestIdentifier];
+ }
}
- (void)requestFailed:(NSString *)requestIdentifier withError:(NSError *)error;
{
NSLog(@"requestFailed:%@", error);
- [self.twitDelegate tweetFailed: requestIdentifier withError: error];
+ if (nil != self.twitDelegate &&
+ [self.twitDelegate respondsToSelector:@selector(tweetFailed:withError:)])
+ {
+ [self.twitDelegate tweetFailed: requestIdentifier withError: error];
+ }
}
- (void)statusesReceived:(NSArray *)statuses forRequest:(NSString *)identifier;
{
NSLog(@"statusesReceived");
- [self.twitDelegate tweetsReceived:statuses forRequest:identifier];
+ if (nil != self.twitDelegate &&
+ [self.twitDelegate respondsToSelector:@selector(tweetsReceived:forRequest:)])
+ {
+ [self.twitDelegate tweetsReceived:statuses forRequest:identifier];
+ }
}
- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)identifier;
View
9 MyTwitterDelegate.h
@@ -8,18 +8,15 @@
#import <Foundation/Foundation.h>
-@protocol MyTwitterDelegate
+@protocol MyTwitterDelegate <NSObject>
@optional
-// the set/get config methods are used to interact with your app so that things like the OAuth token can be saved for later re-use.
- (void) setConfig:(NSString*) value forKey:(NSString*) key;
- (NSString*) configForKey:(NSString*) key;
-
-// called when a tweet is successfully sent
+ - (void) removeConfigForKey:(NSString*) key;
- (void)tweetSucceeded:(NSString *)requestIdentifier;
-// called when a tweet fails
- (void)tweetFailed:(NSString *)requestIdentifier withError:(NSError *)error;
-// called with the tweets following a timeline request
- (void)tweetsReceived:(NSArray *)statuses forRequest:(NSString *)identifier;
+ - (void)twitterAccessfailed;
@end
View
12 README
@@ -11,7 +11,7 @@ Remember to set your Twitter key/secret in the MyTwitter.m file - in fact review
The cient/token/url details are probably as supplied when registering your app with twitter, here : http://twitter.com/oauth_clients/new .
-You need OAuth from Jon Crosby - http://code.google.com/p/oauthconsumer/
+You need OAuth from Jon Crosby - http://code.google.com/p/oauthconsumer/ - note you also need to change this to handle the new oauth_verifier parameter, introduced in 1.0a of OAuth and now used by Twitter.
MGTwitterEngine 1.0.8 with tweaks for OAuth - https://github.com/kimptoc/MGTwitterEngine-1.0.8-OAuth/tree
@@ -49,10 +49,16 @@ When the user closes the browser (need some kind of callback to this)
- (void)browserClosed: (id) thing
{
// this method then goes back to twitter.com and gets the access token, as per OAuth spec.
- // this token is then used in all subsequent twitter interactions to prove this is authorised client.
- [self.mytwitter askForAccessToken];
+
+// user will get shown a pincode, you need to ask the user for this pincode, which is then passed to twitter in the ask for access_token call.
+
}
+Once you have the pincode, ask for the access token
+
+ // this token is then used in all subsequent twitter interactions to prove this is authorised client.
+ [self.mytwitter askForAccessToken: pincode];
+
To send a tweet, you can do it via mytwitter, although the login stuff above means you don't need to:
NSString* connId = [self.mytwitter sendUpdate:tweet];
Please sign in to comment.
Something went wrong with that request. Please try again.