Created
August 9, 2012 21:09
-
-
Save irace/3308095 to your computer and use it in GitHub Desktop.
Objective-C Twitter three-legged authorization flow
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
// | |
// BITwitterAuthenticator.h | |
// | |
// Created by Bryan Irace on 8/9/12. | |
// http://bryan.mit-license.org | |
// | |
@interface BITwitterAuthenticator : NSObject | |
@property (nonatomic, copy) NSString *OAuthConsumerToken; | |
@property (nonatomic, copy) NSString *OAuthConsumerSecret; | |
// 1) Callback parameter: authorization URL (load in an in-app browser or Mobile Safari) | |
- (void)authorizationURL:(void(^)(NSString *))callback; | |
// 2) Twitter will hit your application's callback URL (parameters: request token, verifier) | |
// 3) Callback parameters: OAuth token, secret | |
- (void)accessToken:(NSString *)requestToken verifier:(NSString *)verifier | |
callback:(void(^)(NSString *, NSString *))callback; | |
@end |
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
// | |
// BITwitterAuthenticator.m | |
// | |
// Created by Bryan Irace on 8/9/12. | |
// http://bryan.mit-license.org | |
// | |
#import "BITwitterAuthenticator.h" | |
@implementation BITwitterAuthenticator { | |
HTTPRequestQueue *_requestQueue; | |
} | |
- (void)authorizationURL:(void(^)(NSString *))callback { | |
__block HTTPRequest *request = [_requestQueue postRequestWithPath:@"oauth/request_token"]; | |
request.completionBlock = ^ { | |
if (callback) { | |
NSDictionary *response = request.responseJSON; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
callback([NSString stringWithFormat:@"https://api.twitter.com/oauth/authorize?oauth_token=%@", | |
response[@"oauth_token"]]); | |
}); | |
} | |
}; | |
[_requestQueue addOperation:request]; | |
} | |
- (void)accessToken:(NSString *)requestToken verifier:(NSString *)verifier | |
callback:(void(^)(NSString *, NSString *))callback { | |
__block HTTPRequest *request = [_requestQueue postRequestWithPath:@"oauth/access_token"]; | |
request.OAuthToken = requestToken; | |
[request addParameter:verifier forKey:@"oauth_verifier"]; | |
request.completionBlock = ^ { | |
if (callback) { | |
NSDictionary *response =request.responseJSON; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
callback(response[@"oauth_token"], response[@"oauth_token_secret"]); | |
}); | |
} | |
}; | |
[_requestQueue addOperation:request]; | |
} | |
#pragma mark - NSObject | |
- (id)init { | |
if (self = [super init]) { | |
_requestQueue = [[HTTPRequestQueue alloc] init]; | |
_requestQueue.baseURL = @"https://api.twitter.com"; | |
_requestQueue.maxConcurrentOperationCount = 1; | |
_requestQueue.OAuthConsumerKey = self.OAuthConsumerKey; | |
_requestQueue.OAuthConsumerSecret = self.OAuthConsumerSecret | |
} | |
return self; | |
} | |
- (void)dealloc { | |
[_requestQueue release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment