Permalink
Browse files

Added support for OAuth and an XAuth login API

  • Loading branch information...
1 parent 282b4bf commit c5be05db208a35fe83e727913de89498f912bb44 @stevestreza stevestreza committed Apr 27, 2010
Showing with 108 additions and 22 deletions.
  1. +7 −0 AppController.h
  2. +27 −10 AppController.m
  3. +71 −10 MGTwitterEngine.m
  4. +2 −1 MGTwitterEngineDelegate.h
  5. +1 −1 MGTwitterEngineGlobalHeader.h
View
@@ -9,8 +9,15 @@
#import <Cocoa/Cocoa.h>
#import "MGTwitterEngine.h"
+@class OAToken;
+
@interface AppController : NSObject <MGTwitterEngineDelegate> {
MGTwitterEngine *twitterEngine;
+
+ OAToken *token;
}
+// this gets called when the OAuth token is received
+-(void)runTests;
+
@end
View
@@ -8,7 +8,6 @@
#import "AppController.h"
-
@implementation AppController
@@ -17,17 +16,27 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Put your Twitter username and password here:
NSString *username = nil;
NSString *password = nil;
-
+
+ NSString *consumerKey = nil;
+ NSString *consumerSecret = nil;
+
// Most API calls require a name and password to be set...
- if (! username || ! password) {
- NSLog(@"You forgot to specify your username/password in AppController.m, things might not work!");
+ if (! username || ! password || !consumerKey || !consumerSecret) {
+ NSLog(@"You forgot to specify your username/password/key/secret in AppController.m, things might not work!");
NSLog(@"And if things are mysteriously working without the username/password, it's because NSURLConnection is using a session cookie from another connection.");
}
// Create a TwitterEngine and set our login details.
twitterEngine = [[MGTwitterEngine alloc] initWithDelegate:self];
- [twitterEngine setUsername:username password:password];
-
+ [twitterEngine setUsesSecureConnection:NO];
+ [twitterEngine setConsumerKey:consumerKey secret:consumerSecret];
+
+ [twitterEngine getXAuthAccessTokenForUsername:username password:password];
+}
+
+-(void)runTests{
+ [twitterEngine setAccessToken:token];
+
// Configure how the delegate methods are called to deliver results. See MGTwitterEngineDelegate.h for more info
//[twitterEngine setDeliveryOptions:MGTwitterEngineDeliveryIndividualResultsOption];
@@ -42,11 +51,11 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
#define TESTING_MESSAGE_ID 52182684
// Status methods:
- // NSLog(@"getHomeTimelineFor: connectionIdentifier = %@", [twitterEngine getHomeTimelineSinceID:0 startingAtPage:0 count:20]);
- //NSLog(@"getUserTimelineFor: connectionIdentifier = %@", [twitterEngine getUserTimelineFor:TESTING_SECONDARY_USER sinceID:0 startingAtPage:0 count:3]);
- //NSLog(@"getUpdate: connectionIdentifier = %@", [twitterEngine getUpdate:TESTING_ID]);
+ NSLog(@"getHomeTimelineFor: connectionIdentifier = %@", [twitterEngine getHomeTimelineSinceID:0 startingAtPage:0 count:20]);
+ NSLog(@"getUserTimelineFor: connectionIdentifier = %@", [twitterEngine getUserTimelineFor:TESTING_SECONDARY_USER sinceID:0 startingAtPage:0 count:3]);
+ NSLog(@"getUpdate: connectionIdentifier = %@", [twitterEngine getUpdate:TESTING_ID]);
//NSLog(@"sendUpdate: connectionIdentifier = %@", [twitterEngine sendUpdate:[@"This is a test on " stringByAppendingString:[[NSDate date] description]]]);
- //NSLog(@"getRepliesStartingAtPage: connectionIdentifier = %@", [twitterEngine getRepliesStartingAtPage:0]);
+ NSLog(@"getRepliesStartingAtPage: connectionIdentifier = %@", [twitterEngine getRepliesStartingAtPage:0]);
//NSLog(@"deleteUpdate: connectionIdentifier = %@", [twitterEngine deleteUpdate:TESTING_ID]);
// User methods:
@@ -186,6 +195,14 @@ - (void)connectionFinished:(NSString *)connectionIdentifier
}
}
+- (void)accessTokenReceived:(OAToken *)aToken forRequest:(NSString *)connectionIdentifier
+{
+ NSLog(@"Access token received! %@",aToken);
+
+ token = [aToken retain];
+ [self runTests];
+}
+
#if YAJL_AVAILABLE
- (void)receivedObject:(NSDictionary *)dictionary forRequest:(NSString *)connectionIdentifier
View
@@ -8,7 +8,7 @@
#import "MGTwitterEngine.h"
#import "MGTwitterHTTPURLConnection.h"
-@class OAToken;
+#import "OAuthConsumer.h"
#import "NSData+Base64.h"
@@ -442,7 +442,7 @@ - (NSString *)_sendRequestWithMethod:(NSString *)method
NSMutableURLRequest *theRequest = [self _baseRequestWithMethod:method
path:path
- requestType:requestType
+ requestType:requestType
queryParameters:params];
// Set the request body if this is a POST request.
@@ -614,11 +614,23 @@ - (NSMutableURLRequest *)_baseRequestWithMethod:(NSString *)method
NSLog(@"MGTwitterEngine: finalURL = %@", finalURL);
}
#endif
-
+
// Construct an NSMutableURLRequest for the URL and set appropriate request method.
- NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:finalURL
- cachePolicy:NSURLRequestReloadIgnoringCacheData
- timeoutInterval:URL_REQUEST_TIMEOUT];
+ NSMutableURLRequest *theRequest = nil;
+ if(_accessToken){
+ theRequest = [[[OAMutableURLRequest alloc] initWithURL:finalURL
+ consumer:[[[OAConsumer alloc] initWithKey:[self consumerKey]
+ secret:[self consumerSecret]] autorelease]
+ token:_accessToken
+ realm:nil
+ signatureProvider:nil] autorelease];
+ [theRequest setCachePolicy:NSURLRequestReloadIgnoringCacheData ];
+ [theRequest setTimeoutInterval:URL_REQUEST_TIMEOUT];
+ }else{
+ theRequest = [NSMutableURLRequest requestWithURL:finalURL
+ cachePolicy:NSURLRequestReloadIgnoringCacheData
+ timeoutInterval:URL_REQUEST_TIMEOUT];
+ }
if (method) {
[theRequest setHTTPMethod:method];
}
@@ -639,6 +651,11 @@ - (NSMutableURLRequest *)_baseRequestWithMethod:(NSString *)method
[theRequest setValue:authValue forHTTPHeaderField:@"Authorization"];
}
#endif
+
+ //OAMutableURLRequests need to have prepare called on them
+ if([theRequest respondsToSelector:@selector(prepare)]){
+ [theRequest prepare];
+ }
return theRequest;
}
@@ -690,7 +707,10 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
connectionIdentifier:identifier requestType:requestType
responseType:responseType URL:URL deliveryOptions:_deliveryOptions];
break;
- case MGTwitterOAuthTokenRequest:
+ case MGTwitterOAuthToken:;
+ OAToken *token = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
+ [self parsingSucceededForRequest:identifier ofResponseType:requestType
+ withParsedObjects:[NSArray arrayWithObject:token]];
break;
default:
break;
@@ -736,6 +756,9 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
connectionIdentifier:identifier requestType:requestType
responseType:responseType URL:URL];
break;
+ case MGTwitterOAuthTokenRequest:
+ NSLog(@"Something with OAuth!");
+ break;
default:
break;
}
@@ -769,6 +792,9 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
[MGTwitterSocialGraphParser parserWithXML:xmlData delegate:self
connectionIdentifier:identifier requestType:requestType
responseType:responseType];
+ case MGTwitterOAuthTokenRequest:
+ NSLog(@"Something with OAuth!");
+ break;
default:
break;
}
@@ -820,6 +846,11 @@ - (void)parsingSucceededForRequest:(NSString *)identifier
if ([self _isValidDelegateForSelector:@selector(socialGraphInfoReceived:forRequest:)])
[_delegate socialGraphInfoReceived: parsedObjects forRequest:identifier];
break;
+ case MGTwitterOAuthTokenRequest:
+ if ([self _isValidDelegateForSelector:@selector(accessTokenReceived:forRequest:)] && [parsedObjects count] > 0)
+ [_delegate accessTokenReceived:[parsedObjects objectAtIndex:0]
+ forRequest:identifier];
+ break;
default:
break;
}
@@ -1914,10 +1945,40 @@ - (OAToken *)accessToken{
- (NSString *)getXAuthAccessTokenForUsername:(NSString *)username
password:(NSString *)password{
- return [self _sendRequestWithMethod:nil path:path queryParameters:params body:nil
- requestType:MGTwitterOAuthGetXAuthAccessToken
- responseType:MGTwitterOAuthToken];
+ OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:[self consumerKey] secret:[self consumerSecret]] autorelease];
+
+ OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"]
+ consumer:consumer
+ token:nil // xAuth needs no request token?
+ realm:nil // our service provider doesn't specify a realm
+ signatureProvider:nil]; // use the default method, HMAC-SHA1
+
+ [request setHTTPMethod:@"POST"];
+ [request setParameters:[NSArray arrayWithObjects:
+ [OARequestParameter requestParameter:@"x_auth_mode" value:@"client_auth"],
+ [OARequestParameter requestParameter:@"x_auth_username" value:username],
+ [OARequestParameter requestParameter:@"x_auth_password" value:password],
+ nil]];
@nearfri

nearfri Jul 6, 2010

don't you mean:

[OARequestParameter requestParameterWithName:@"x_auth_mode" value:@"client_auth"],

+
+ [request prepare];
+
+ // Create a connection using this request, with the default timeout and caching policy,
+ // and appropriate Twitter request and response types for parsing and error reporting.
+ MGTwitterHTTPURLConnection *connection;
+ connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:request
+ delegate:self
+ requestType:MGTwitterOAuthTokenRequest
+ responseType:MGTwitterOAuthToken];
+
+ if (!connection) {
+ return nil;
+ } else {
+ [_connections setObject:connection forKey:[connection identifier]];
+ [connection release];
+ }
+
+ return [connection identifier];
}
@end
@@ -7,7 +7,7 @@
//
#import "MGTwitterEngineGlobalHeader.h"
-
+@class OAToken;
typedef enum _MGTwitterEngineDeliveryOptions {
// all results will be delivered as an array via statusesReceived: and similar delegate methods
@@ -46,6 +46,7 @@ typedef enum _MGTwitterEngineDeliveryOptions {
- (void)searchResultsReceived:(NSArray *)searchResults forRequest:(NSString *)connectionIdentifier;
#endif
- (void)socialGraphInfoReceived:(NSArray *)socialGraphInfo forRequest:(NSString *)connectionIdentifier;
+- (void)accessTokenReceived:(OAToken *)token forRequest:(NSString *)connectionIdentifier;
#if TARGET_OS_IPHONE
- (void)imageReceived:(UIImage *)image forRequest:(NSString *)connectionIdentifier;
@@ -48,7 +48,7 @@
target.
*/
-#define YAJL_AVAILABLE 0
+#define YAJL_AVAILABLE 1
#if YAJL_AVAILABLE
/*

0 comments on commit c5be05d

Please sign in to comment.