Browse files

Clean up code for STwitterOAuth

  • Loading branch information...
1 parent 2107ad4 commit 96d5d68a6b56f356cde6eda5a5b6bead8a255a02 @sinoru committed Mar 25, 2012
Showing with 109 additions and 46 deletions.
  1. +5 −3 STwitter/STwitterOAuth.h
  2. +100 −39 STwitter/STwitterOAuth.m
  3. +2 −2 STwitter/STwitterRequest.h
  4. +2 −2 STwitter/STwitterRequest.m
View
8 STwitter/STwitterOAuth.h
@@ -13,8 +13,10 @@
}
-+ (NSDictionary *)requestRequestTokenWithOAuthConsumerKey:oAuthConsumerKey oAuthConsumerSecret:oAuthConsumerSecret;
-+ (NSURLRequest *)authorizeURLRequestWithRequestToken:(NSString *)token;
-+ (NSDictionary *)exchangeRequestTokenToAccessTokenWithOAuthConsumerKey:oAuthConsumerKey oAuthConsumerSecret:oAuthConsumerSecret oAuthRequestToken:(NSString *)oAuthRequestToken oAuthRequestTokenSecret:(NSString *)oAuthRequestTokenSecret oAuthVerifier:oAuthVerifier;
++ (NSDictionary *)getRequestTokenWithOAuthConsumerKey:OAuthConsumerKey OAuthConsumerSecret:OAuthConsumerSecret;
++ (NSURLRequest *)getUserAuthorizeURLRequestWithRequestToken:(NSString *)token;
++ (NSURLRequest *)getUserAuthorizeURLRequestWithRequestToken:(NSString *)token forceLogin:(BOOL)forceLogin screenName:(NSString *)screenName;
++ (NSDictionary *)exchangeRequestTokenForAccessTokenWithOAuthConsumerKey:OAuthConsumerKey OAuthConsumerSecret:OAuthConsumerSecret OAuthRequestToken:(NSString *)OAuthRequestToken OAuthRequestTokenSecret:(NSString *)OAuthRequestTokenSecret OAuthVerifier:OAuthVerifier;
++ (NSDictionary *)exchangeRequestTokenForAccessTokenWithOAuthConsumerKey:OAuthConsumerKey OAuthConsumerSecret:OAuthConsumerSecret OAuthRequestToken:(NSString *)OAuthRequestToken OAuthRequestTokenSecret:(NSString *)OAuthRequestTokenSecret xAuthUsername:(NSString *)xAuthUsername xAuthPassword:(NSString *)xAuthPassword;
@end
View
139 STwitter/STwitterOAuth.m
@@ -14,27 +14,27 @@
@implementation STwitterOAuth
-+ (NSDictionary *)requestRequestTokenWithOAuthConsumerKey:oAuthConsumerKey oAuthConsumerSecret:oAuthConsumerSecret
++ (NSDictionary *)getRequestTokenWithOAuthConsumerKey:OAuthConsumerKey OAuthConsumerSecret:OAuthConsumerSecret
{
// Declare Variables
- NSString *oAuthNonce;
- NSString *oAuthTimestamp;
- NSString *oAuthArgumentString;
+ NSString *OAuthNonce;
+ NSString *OAuthTimestamp;
+ NSString *OAuthArgumentString;
NSURL *apiURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/request_token"];
// Generate UUID for OAuth Nonce
- oAuthNonce = [STwitterOAuthTool generateUUID];
+ OAuthNonce = [STwitterOAuthTool generateUUID];
// Generate Time Stamp
- oAuthTimestamp = [NSString stringWithFormat:@"%i" , [[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]] intValue]];
+ OAuthTimestamp = [NSString stringWithFormat:@"%i" , [[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]] intValue]];
- NSMutableDictionary *oAuthArgumentDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"oob", @"oauth_callback", oAuthConsumerKey, @"oauth_consumer_key", oAuthNonce, @"oauth_nonce", @"HMAC-SHA1", @"oauth_signature_method", oAuthTimestamp, @"oauth_timestamp", @"1.0", @"oauth_version", nil];
+ NSMutableDictionary *oAuthArgumentDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"oob", @"oauth_callback", OAuthConsumerKey, @"oauth_consumer_key", OAuthNonce, @"oauth_nonce", @"HMAC-SHA1", @"oauth_signature_method", OAuthTimestamp, @"oauth_timestamp", @"1.0", @"oauth_version", nil];
// Generate and Add OAuthTokenSignature
- [oAuthArgumentDict setObject:[STwitterOAuthTool generateOAuthSignature:oAuthArgumentDict httpMethod:@"POST" apiURL:apiURL oAuthConsumerSecret:oAuthConsumerSecret oAuthTokenSecret:nil] forKey:@"oauth_signature"];
+ [oAuthArgumentDict setObject:[STwitterOAuthTool generateOAuthSignature:oAuthArgumentDict httpMethod:@"POST" apiURL:apiURL oAuthConsumerSecret:OAuthConsumerSecret oAuthTokenSecret:nil] forKey:@"oauth_signature"];
// Generate HTTP Authorization Header String
- oAuthArgumentString = [STwitterOAuthTool generateHTTPAuthorizationHeader:oAuthArgumentDict];
+ OAuthArgumentString = [STwitterOAuthTool generateHTTPAuthorizationHeader:oAuthArgumentDict];
// Create Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:apiURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
@@ -43,64 +43,125 @@ + (NSDictionary *)requestRequestTokenWithOAuthConsumerKey:oAuthConsumerKey oAuth
[request setHTTPMethod:@"POST"];
// Set HTTP Authorization Header to requestsParameter
- [request setValue:oAuthArgumentString forHTTPHeaderField:@"Authorization"];
+ [request setValue:OAuthArgumentString forHTTPHeaderField:@"Authorization"];
// Get Token
- NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- NSMutableDictionary *returnDict = nil;
- if (returnData) {
- NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
+ NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ NSMutableDictionary *parsedDict = nil;
+ if (receivedData) {
+ NSString *returnString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
if (returnString) {
- NSArray *returnArray = [returnString componentsSeparatedByString:@"&"];
- if (returnArray) {
- returnDict = [[NSMutableDictionary alloc] init];
+ NSArray *parsedArray = [returnString componentsSeparatedByString:@"&"];
+ if (parsedArray) {
+ parsedDict = [[NSMutableDictionary alloc] init];
- for(id argument in returnArray)
+ for(id argument in parsedArray)
{
@autoreleasepool {
NSArray *argumentKeyAndObject = [argument componentsSeparatedByString:@"="];
- [returnDict setObject:[argumentKeyAndObject objectAtIndex:1] forKey:[argumentKeyAndObject objectAtIndex:0]];
+ [parsedDict setObject:[argumentKeyAndObject objectAtIndex:1] forKey:[argumentKeyAndObject objectAtIndex:0]];
}
}
}
}
}
- return returnDict;
+ return [NSDictionary dictionaryWithObjectsAndKeys:[parsedDict objectForKey:@"oauth_token"], @"OAuthRequestToken", [parsedDict objectForKey:@"oauth_token_secret"], @"OAuthRequestTokenSecret", nil];
}
-+ (NSURLRequest *)authorizeURLRequestWithRequestToken:(NSString *)token
++ (NSURLRequest *)getUserAuthorizeURLRequestWithRequestToken:(NSString *)token
{
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/oauth/authorize?oauth_token=%@", token]] cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:5.0f];
+ return [self getUserAuthorizeURLRequestWithRequestToken:token forceLogin:NO screenName:nil];
+}
+
++ (NSURLRequest *)getUserAuthorizeURLRequestWithRequestToken:(NSString *)token forceLogin:(BOOL)forceLogin screenName:(NSString *)screenName
+{
+ NSURL *apiURL;
+ if (screenName)
+ apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/oauth/authorize?oauth_token=%@&force_login=%@&screen_name=%@", token, forceLogin ? @"true" : @"false", screenName]];
+ else
+ apiURL = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/oauth/authorize?oauth_token=%@&force_login=%@", token, forceLogin ? @"true" : @"false"]];
+
+ NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:apiURL cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:5.0f];
// Set HTTP Method to GET
[request setHTTPMethod:@"GET"];
return request;
}
-+ (NSDictionary *)exchangeRequestTokenToAccessTokenWithOAuthConsumerKey:oAuthConsumerKey oAuthConsumerSecret:oAuthConsumerSecret oAuthRequestToken:(NSString *)oAuthRequestToken oAuthRequestTokenSecret:(NSString *)oAuthRequestTokenSecret oAuthVerifier:oAuthVerifier
+
++ (NSDictionary *)exchangeRequestTokenForAccessTokenWithOAuthConsumerKey:OAuthConsumerKey OAuthConsumerSecret:OAuthConsumerSecret OAuthRequestToken:(NSString *)OAuthRequestToken OAuthRequestTokenSecret:(NSString *)OAuthRequestTokenSecret OAuthVerifier:OAuthVerifier
+{
+ // Declare Variables
+ NSString *OAuthNonce;
+ NSString *OAuthTimestamp;
+ NSString *OAuthArgumentString;
+ NSURL *apiURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
+
+ // Generate UUID for OAuth Nonce
+ OAuthNonce = [STwitterOAuthTool generateUUID];
+
+ // Generate Time Stamp
+ OAuthTimestamp = [NSString stringWithFormat:@"%i" , [[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]] intValue]];
+
+ NSMutableDictionary *OAuthArgumentDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:OAuthConsumerKey, @"oauth_consumer_key", OAuthNonce, @"oauth_nonce", @"HMAC-SHA1", @"oauth_signature_method", OAuthRequestToken, @"oauth_token", OAuthTimestamp, @"oauth_timestamp", @"1.0", @"oauth_version", OAuthVerifier, @"oauth_verifier", nil];
+
+ // Generate and Add OAuthTokenSignature
+ [OAuthArgumentDict setObject:[STwitterOAuthTool generateOAuthSignature:OAuthArgumentDict httpMethod:@"POST" apiURL:apiURL oAuthConsumerSecret:OAuthConsumerSecret oAuthTokenSecret:OAuthRequestTokenSecret] forKey:@"oauth_signature"];
+
+ // Generate HTTP Authorization Header String
+ OAuthArgumentString = [STwitterOAuthTool generateHTTPAuthorizationHeader:OAuthArgumentDict];
+
+ // Create Request
+ NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:apiURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
+
+ // Set HTTP Method to POST
+ [request setHTTPMethod:@"POST"];
+
+ // Set HTTP Authorization Header to requestsParameter
+ [request setValue:OAuthArgumentString forHTTPHeaderField:@"Authorization"];
+
+ // Get Token
+ NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ NSString *returnString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
+
+ NSArray *parsedArray = [returnString componentsSeparatedByString:@"&"];
+ NSMutableDictionary *parsedDict = [[NSMutableDictionary alloc] init];
+
+ for(id argument in parsedArray)
+ {
+ @autoreleasepool {
+ NSArray *argumentKeyAndObject = [argument componentsSeparatedByString:@"="];
+ [parsedDict setObject:[argumentKeyAndObject objectAtIndex:1] forKey:[argumentKeyAndObject objectAtIndex:0]];
+ }
+ }
+
+ return [NSDictionary dictionaryWithObjectsAndKeys:[parsedDict objectForKey:@"oauth_token"], @"OAuthAccessToken", [parsedDict objectForKey:@"oauth_token_secret"], @"OAuthAccessTokenSecret", nil];
+}
+
++ (NSDictionary *)exchangeRequestTokenForAccessTokenWithOAuthConsumerKey:OAuthConsumerKey OAuthConsumerSecret:OAuthConsumerSecret OAuthRequestToken:(NSString *)OAuthRequestToken OAuthRequestTokenSecret:(NSString *)OAuthRequestTokenSecret xAuthUsername:(NSString *)xAuthUsername xAuthPassword:(NSString *)xAuthPassword
{
// Declare Variables
- NSString *oAuthNonce;
- NSString *oAuthTimestamp;
- NSString *oAuthArgumentString;
+ NSString *OAuthNonce;
+ NSString *OAuthTimestamp;
+ NSString *OAuthArgumentString;
NSURL *apiURL = [NSURL URLWithString:@"https://api.twitter.com/oauth/access_token"];
// Generate UUID for OAuth Nonce
- oAuthNonce = [STwitterOAuthTool generateUUID];
+ OAuthNonce = [STwitterOAuthTool generateUUID];
// Generate Time Stamp
- oAuthTimestamp = [NSString stringWithFormat:@"%i" , [[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]] intValue]];
+ OAuthTimestamp = [NSString stringWithFormat:@"%i" , [[NSNumber numberWithDouble:[[NSDate date] timeIntervalSince1970]] intValue]];
- NSMutableDictionary *oAuthArgumentDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:oAuthConsumerKey, @"oauth_consumer_key", oAuthNonce, @"oauth_nonce", @"HMAC-SHA1", @"oauth_signature_method", oAuthRequestToken, @"oauth_token", oAuthTimestamp, @"oauth_timestamp", @"1.0", @"oauth_version", nil];
+ NSMutableDictionary *OAuthArgumentDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:OAuthConsumerKey, @"oauth_consumer_key", OAuthNonce, @"oauth_nonce", @"HMAC-SHA1", @"oauth_signature_method", OAuthRequestToken, @"oauth_token", OAuthTimestamp, @"oauth_timestamp", @"1.0", @"oauth_version", xAuthPassword, @"x_auth_password", xAuthUsername, @"x_auth_username", @"client_auth", @"x_auth_mode", nil];
// Generate and Add OAuthTokenSignature
- [oAuthArgumentDict setObject:[STwitterOAuthTool generateOAuthSignature:oAuthArgumentDict httpMethod:@"POST" apiURL:apiURL oAuthConsumerSecret:oAuthConsumerSecret oAuthTokenSecret:oAuthRequestTokenSecret] forKey:@"oauth_signature"];
+ [OAuthArgumentDict setObject:[STwitterOAuthTool generateOAuthSignature:OAuthArgumentDict httpMethod:@"POST" apiURL:apiURL oAuthConsumerSecret:OAuthConsumerSecret oAuthTokenSecret:OAuthRequestTokenSecret] forKey:@"oauth_signature"];
// Generate HTTP Authorization Header String
- oAuthArgumentString = [STwitterOAuthTool generateHTTPAuthorizationHeader:oAuthArgumentDict];
+ OAuthArgumentString = [STwitterOAuthTool generateHTTPAuthorizationHeader:OAuthArgumentDict];
// Create Request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:apiURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0f];
@@ -109,24 +170,24 @@ + (NSDictionary *)exchangeRequestTokenToAccessTokenWithOAuthConsumerKey:oAuthCon
[request setHTTPMethod:@"POST"];
// Set HTTP Authorization Header to requestsParameter
- [request setValue:oAuthArgumentString forHTTPHeaderField:@"Authorization"];
+ [request setValue:OAuthArgumentString forHTTPHeaderField:@"Authorization"];
// Get Token
- NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
- NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
+ NSData *receivedData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
+ NSString *returnString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
- NSArray *returnArray = [returnString componentsSeparatedByString:@"&"];
- NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] init];
+ NSArray *parsedArray = [returnString componentsSeparatedByString:@"&"];
+ NSMutableDictionary *parsedDict = [[NSMutableDictionary alloc] init];
- for(id argument in returnArray)
+ for(id argument in parsedArray)
{
@autoreleasepool {
NSArray *argumentKeyAndObject = [argument componentsSeparatedByString:@"="];
- [returnDict setObject:[argumentKeyAndObject objectAtIndex:1] forKey:[argumentKeyAndObject objectAtIndex:0]];
+ [parsedDict setObject:[argumentKeyAndObject objectAtIndex:1] forKey:[argumentKeyAndObject objectAtIndex:0]];
}
}
- return returnDict;
+ return [NSDictionary dictionaryWithObjectsAndKeys:[parsedDict objectForKey:@"oauth_token"], @"OAuthAccessToken", [parsedDict objectForKey:@"oauth_token_secret"], @"OAuthAccessTokenSecret", nil];
}
@end
View
4 STwitter/STwitterRequest.h
@@ -34,8 +34,8 @@ typedef void(^STwitterRequestHandler)(NSData *responseData, NSHTTPURLResponse *u
/**
* Optional account information used to authenticate the request. If you don't want using ACAccount.
- * This should be a dictionary that include keys named "OAuthConsumerKey", "OAuthConsumerSecret", "OAuthAccessToken",
- * "OAuthAccessTokenSecret" and NSString objects.
+ * This should be a dictionary that include keys named "OAuthConsumerKey", "OAuthConsumerSecret", "OAuthToken",
+ * "OAuthTokenSecret" and NSString objects.
*/
@property (nonatomic, retain) NSDictionary *OAuthToken;
View
4 STwitter/STwitterRequest.m
@@ -109,8 +109,8 @@ - (NSURLRequest *)signedURLRequest
NSString *OAuthConsumerKey = [OAuthToken objectForKey:@"OAuthConsumerKey"];
NSString *OAuthConsumerSecret = [OAuthToken objectForKey:@"OAuthConsumerSecret"];
- NSString *OAuthAccessToken = [OAuthToken objectForKey:@"OAuthAccessToken"];
- NSString *OAuthAccessTokenSecret = [OAuthToken objectForKey:@"OAuthAccessTokenSecret"];
+ NSString *OAuthAccessToken = [OAuthToken objectForKey:@"OAuthToken"];
+ NSString *OAuthAccessTokenSecret = [OAuthToken objectForKey:@"OAuthTokenSecret"];
NSString *OAuthSignatureMethod = @"HMAC-SHA1";
NSString *OAuthVersion = @"1.0";

0 comments on commit 96d5d68

Please sign in to comment.