Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented request_token -> PIN -> auth_token #60

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*.mode1v3
build
yajl
OAuthConsumer
SecretValues.h
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "OAuthConsumer"]
path = OAuthConsumer
url = [email protected]:fxtentacle/oauthconsumer.git
5 changes: 5 additions & 0 deletions AppController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,14 @@
MGTwitterEngine *twitterEngine;

OAToken *token;

IBOutlet NSTextField* pinCode;
IBOutlet NSWindow* pinWindow;
}

// this gets called when the OAuth token is received
-(void)runTests;

- (IBAction)enteredPin: (id)sender;

@end
31 changes: 23 additions & 8 deletions AppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Put your Twitter username and password here:
NSString *username = nil;
NSString *password = nil;

NSString *consumerKey = nil;
NSString *consumerSecret = nil;

// this file will set username, consumerKey and consumerSecret
#include "SecretValues.h"

// Most API calls require a name and password to be set...
if (! username || ! password || !consumerKey || !consumerSecret) {
if (! username || !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.");
}
Expand All @@ -34,7 +36,25 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// At present the list API calls require you to specify a user that owns the list.
[twitterEngine setUsername:username];

[twitterEngine getXAuthAccessTokenForUsername:username password:password];
[twitterEngine getRequestToken];
}


- (void)requestTokenReceived:(OAToken *)aToken forRequest:(NSString *)connectionIdentifier
{
NSLog(@"Request token received! %@",aToken);
token = [aToken retain];

[twitterEngine openAuthorizePageForRequestToken: token];

[NSBundle loadNibNamed:@"PinWindow" owner:self];
// NSWindowController* awc = [[NSWindowController alloc] initWithWindowNibName:@"PinWindow"];
}

-(IBAction)enteredPin: (id)sender
{
[twitterEngine getAccessTokenForRequestToken: token pin: [pinCode stringValue]];
[pinWindow close];
}

-(void)runTests{
Expand Down Expand Up @@ -203,11 +223,6 @@ - (void)imageReceived:(NSImage *)image forRequest:(NSString *)connectionIdentifi
- (void)connectionFinished:(NSString *)connectionIdentifier
{
NSLog(@"Connection finished %@", connectionIdentifier);

if ([twitterEngine numberOfConnections] == 0)
{
[NSApp terminate:self];
}
}

- (void)accessTokenReceived:(OAToken *)aToken forRequest:(NSString *)connectionIdentifier
Expand Down
28 changes: 28 additions & 0 deletions Demo-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>NSMainNibFile</key>
<string>MainMenu</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
</dict>
</plist>
22 changes: 22 additions & 0 deletions MGTwitterEngine-Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIdentifier</key>
<string>com.yourcompany.${PRODUCT_NAME:rfc1034identifier}</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
5 changes: 5 additions & 0 deletions MGTwitterEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@
- (NSString *)getXAuthAccessTokenForUsername:(NSString *)username
password:(NSString *)password;

- (NSString *)getRequestToken;
- (NSString *)openAuthorizePageForRequestToken: (OAToken *)requestToken;

- (NSString *)getAccessTokenForRequestToken: (OAToken *)requestToken pin: (NSString* )pin;

@end


119 changes: 117 additions & 2 deletions MGTwitterEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
connectionIdentifier:identifier requestType:requestType
responseType:responseType URL:URL deliveryOptions:_deliveryOptions];
break;
case MGTwitterAccountInfo:
case MGTwitterUsers:
case MGTwitterUser:
[MGTwitterUsersYAJLParser parserWithJSON:jsonData delegate:self
Expand Down Expand Up @@ -760,7 +761,12 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
break;
default:
case MGTwitterOAuthRequestToken:;
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;
}
#elif TOUCHJSON_AVAILABLE
Expand All @@ -770,6 +776,11 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
break;
case MGTwitterOAuthRequestToken:;
OAToken *token = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
break;
default:
[MGTwitterTouchJSONParser parserWithJSON:jsonData delegate:self
connectionIdentifier:identifier requestType:requestType
Expand Down Expand Up @@ -797,6 +808,7 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
connectionIdentifier:identifier requestType:requestType
responseType:responseType URL:URL];
break;
case MGTwitterAccountInfo:
case MGTwitterUsers:
case MGTwitterUser:
[MGTwitterUsersLibXMLParser parserWithXML:xmlData delegate:self
Expand All @@ -823,6 +835,12 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
OAToken *token = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
break;
case MGTwitterOAuthRequestToken:;
OAToken *token = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
break;
default:
break;
}
Expand All @@ -835,6 +853,7 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
connectionIdentifier:identifier requestType:requestType
responseType:responseType];
break;
case MGTwitterAccountInfo:
case MGTwitterUsers:
case MGTwitterUser:
[MGTwitterUsersParser parserWithXML:xmlData delegate:self
Expand Down Expand Up @@ -867,6 +886,12 @@ - (void)_parseDataForConnection:(MGTwitterHTTPURLConnection *)connection
OAToken *token = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:token]];
break;
case MGTwitterOAuthRequestToken:;
OAToken *rtoken = [[[OAToken alloc] initWithHTTPResponseBody:[[[NSString alloc] initWithData:xmlData encoding:NSUTF8StringEncoding] autorelease]] autorelease];
[self parsingSucceededForRequest:identifier ofResponseType:requestType
withParsedObjects:[NSArray arrayWithObject:rtoken]];
break;
default:
break;
}
Expand Down Expand Up @@ -928,6 +953,15 @@ - (void)parsingSucceededForRequest:(NSString *)identifier
[_delegate accessTokenReceived:[parsedObjects objectAtIndex:0]
forRequest:identifier];
break;
case MGTwitterOAuthRequestTokenRequest:
if ([self _isValidDelegateForSelector:@selector(requestTokenReceived:forRequest:)] && [parsedObjects count] > 0)
[_delegate requestTokenReceived:[parsedObjects objectAtIndex:0]
forRequest:identifier];
break;
case MGTwitterAccountInfo:
if ([self _isValidDelegateForSelector:@selector(accountInfoReceived:forRequest:)])
[_delegate accountInfoReceived:[parsedObjects objectAtIndex:0] forRequest:identifier];
break;
default:
break;
}
Expand Down Expand Up @@ -1684,7 +1718,7 @@ - (NSString *)checkUserCredentials

return [self _sendRequestWithMethod:nil path:path queryParameters:nil body:nil
requestType:MGTwitterAccountRequest
responseType:MGTwitterUser];
responseType:MGTwitterAccountInfo];
}


Expand Down Expand Up @@ -2163,6 +2197,87 @@ - (NSString *)getXAuthAccessTokenForUsername:(NSString *)username
return [connection identifier];
}

- (NSString *)getRequestToken {
OAConsumer *consumer = [[[OAConsumer alloc] initWithKey:[self consumerKey] secret:[self consumerSecret]] autorelease];

OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"]
consumer:consumer
token:nil // we want to know the 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: nil]];

// 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:MGTwitterOAuthRequestTokenRequest
responseType:MGTwitterOAuthRequestToken];

[request release], request = nil;

if (!connection) {
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}

if ([self _isValidDelegateForSelector:@selector(connectionStarted:)])
[_delegate connectionStarted:[connection identifier]];

return [connection identifier];
}

- (NSString *)openAuthorizePageForRequestToken: (OAToken *)requestToken {
NSString* str = [requestToken key];
NSURL* url = [NSURL URLWithString: [@"https://api.twitter.com/oauth/authorize?oauth_token=" stringByAppendingString:[str encodedURLParameterString]] ];
[[NSWorkspace sharedWorkspace] openURL:url];
return [url absoluteString];
}

- (NSString *)getAccessTokenForRequestToken: (OAToken *)requestToken pin: (NSString* )pin {
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:requestToken // 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:@"oauth_verifier" value:pin],
nil]];

// 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];

[request release], request = nil;

if (!connection) {
return nil;
} else {
[_connections setObject:connection forKey:[connection identifier]];
[connection release];
}

if ([self _isValidDelegateForSelector:@selector(connectionStarted:)])
[_delegate connectionStarted:[connection identifier]];

return [connection identifier];
}

@end


Loading