diff --git a/AFOAuth1Client.m b/AFOAuth1Client.m index 4a4c199..d72541b 100644 --- a/AFOAuth1Client.m +++ b/AFOAuth1Client.m @@ -194,13 +194,6 @@ - (id)initWithQueryString:(NSString *)queryString { @interface AFOAuth1Client () @property (readwrite, nonatomic, copy) NSString *key; @property (readwrite, nonatomic, copy) NSString *secret; - -- (void) signCallPerAuthHeaderWithPath:(NSString *)path - andParameters:(NSDictionary *)parameters - andMethod:(NSString *)method ; -- (NSDictionary *) signCallWithHttpGetWithPath:(NSString *)path - andParameters:(NSDictionary *)parameters - andMethod:(NSString *)method ; @end @implementation AFOAuth1Client @@ -247,6 +240,8 @@ - (void)authorizeUsingOAuthWithRequestTokenPath:(NSString *)requestTokenPath currentRequestToken.verifier = [AFParametersFromQueryString([url query]) valueForKey:@"oauth_verifier"]; [self acquireOAuthAccessTokenWithPath:accessTokenPath requestToken:currentRequestToken accessMethod:accessMethod success:^(AFOAuth1Token * accessToken) { + self.accessToken = accessToken; + if (success) { success(accessToken); } @@ -291,7 +286,7 @@ - (NSString *)OAuthSignatureForMethod:(NSString *)method parameters:(NSDictionary *)parameters requestToken:(AFOAuth1Token *)requestToken { - NSMutableURLRequest *request = [self requestWithMethod:@"HEAD" path:path parameters:parameters]; + NSMutableURLRequest *request = [super requestWithMethod:@"HEAD" path:path parameters:parameters]; [request setHTTPMethod:method]; switch (self.signatureMethod) { @@ -312,12 +307,9 @@ - (void)acquireOAuthRequestTokenWithPath:(NSString *)path { NSMutableDictionary *parameters = [[self OAuthParameters] mutableCopy]; [parameters setValue:[callbackURL absoluteString] forKey:@"oauth_callback"]; - - [parameters setValue:[self OAuthSignatureForMethod:accessMethod path:path parameters:parameters requestToken:nil] forKey:@"oauth_signature"]; NSMutableURLRequest *request = [self requestWithMethod:accessMethod path:path parameters:parameters]; [request setHTTPBody:nil]; - [request setValue:[self authorizationHeaderForParameters:parameters] forHTTPHeaderField:@"Authorization"]; AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { @@ -343,10 +335,7 @@ - (void)acquireOAuthAccessTokenWithPath:(NSString *)path [parameters setValue:requestToken.key forKey:@"oauth_token"]; [parameters setValue:requestToken.verifier forKey:@"oauth_verifier"]; - [parameters setValue:[self OAuthSignatureForMethod:accessMethod path:path parameters:parameters requestToken:requestToken] forKey:@"oauth_signature"]; - NSMutableURLRequest *request = [self requestWithMethod:accessMethod path:path parameters:parameters]; - [request setValue:[self authorizationHeaderForParameters:parameters] forHTTPHeaderField:@"Authorization"]; AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { if (success) { @@ -363,6 +352,10 @@ - (void)acquireOAuthAccessTokenWithPath:(NSString *)path } - (NSString *)authorizationHeaderForParameters:(NSDictionary *)parameters { + if (!parameters) { + return nil; + } + NSArray *sortedComponents = [[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) componentsSeparatedByString:@"&"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; NSMutableArray *mutableComponents = [NSMutableArray array]; for (NSString *component in sortedComponents) { @@ -375,49 +368,26 @@ - (NSString *)authorizationHeaderForParameters:(NSDictionary *)parameters { #pragma mark - AFHTTPClient + - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters { + NSMutableDictionary *mutableParameters = parameters ? [parameters mutableCopy] : [NSMutableDictionary dictionary]; + + if (self.accessToken) { + [mutableParameters addEntriesFromDictionary:[self OAuthParameters]]; + [mutableParameters setValue:self.accessToken.key forKey:@"oauth_token"]; + } + + [mutableParameters setValue:[self OAuthSignatureForMethod:method path:path parameters:parameters requestToken:nil] forKey:@"oauth_signature"]; + + NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters]; + [request setValue:[self authorizationHeaderForParameters:mutableParameters] forHTTPHeaderField:@"Authorization"]; [request setHTTPShouldHandleCookies:NO]; return request; } -//- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest -// success:(void (^)(AFHTTPRequestOperation *, id))success -// failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure -//{ -// if (self.accessToken) { -// if ([self.oauthAccessMethod isEqualToString:@"GET"]) -// parameters = [self signCallWithHttpGetWithPath:path andParameters:parameters andMethod:@"GET"]; -// else -// [self signCallPerAuthHeaderWithPath:path andParameters:parameters andMethod:@"GET"]; -// } -// -// AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:urlRequest success:success failure:failure]; -//} - -//- (void) signCallPerAuthHeaderWithPath:(NSString *)path usingParameters:(NSMutableDictionary *)parameters andMethod:(NSString *)method { -// NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:path parameters:parameters]; -// [request setHTTPMethod:method]; -//// [parameters setValue:AFSignatureUsingMethodWithSignatureWithConsumerSecretAndRequestTokenSecret(request, self.signatureMethod, self.secret, self.accessToken.secret, self.stringEncoding) forKey:@"oauth_signature"]; -// -// -// NSArray *sortedComponents = [[AFQueryStringFromParametersWithEncoding(parameters, self.stringEncoding) componentsSeparatedByString:@"&"] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; -// NSMutableArray *mutableComponents = [NSMutableArray array]; -// for (NSString *component in sortedComponents) { -// NSArray *subcomponents = [component componentsSeparatedByString:@"="]; -// [mutableComponents addObject:[NSString stringWithFormat:@"%@=\"%@\"", subcomponents[0], subcomponents[1]]]; -// } -// -// NSString *oauthString = [NSString stringWithFormat:@"OAuth %@", [mutableComponents componentsJoinedByString:@", "]]; -// -// NSLog(@"OAuth: %@", oauthString); -// -// [self setDefaultHeader:@"Authorization" value:oauthString]; -//} -// - @end diff --git a/Twitter OSX Example Client/Twitter OSX Example Client.xcodeproj/project.pbxproj b/Twitter OSX Example Client/Twitter OSX Example Client.xcodeproj/project.pbxproj index f0442c4..f1372e7 100644 --- a/Twitter OSX Example Client/Twitter OSX Example Client.xcodeproj/project.pbxproj +++ b/Twitter OSX Example Client/Twitter OSX Example Client.xcodeproj/project.pbxproj @@ -80,6 +80,7 @@ 3B951FF916944D5B00BAF7F8 /* Twitter OSX Example Client */, 3B951FF216944D5B00BAF7F8 /* Frameworks */, 3B951FF016944D5B00BAF7F8 /* Products */, + F86035CE16ADCCC60027FDE8 /* Vendor */, ); sourceTree = ""; }; @@ -113,7 +114,6 @@ 3B951FF916944D5B00BAF7F8 /* Twitter OSX Example Client */ = { isa = PBXGroup; children = ( - 3B95201316944D7900BAF7F8 /* AFNetworking */, 3B95201016944D7000BAF7F8 /* AFOAuth1Client.h */, 3B95201116944D7000BAF7F8 /* AFOAuth1Client.m */, 3B95200516944D5B00BAF7F8 /* AppDelegate.h */, @@ -160,7 +160,15 @@ 3B95202616944D7900BAF7F8 /* UIImageView+AFNetworking.m */, ); name = AFNetworking; - path = ../../AFNetworking/AFNetworking; + path = ../AFNetworking/AFNetworking; + sourceTree = ""; + }; + F86035CE16ADCCC60027FDE8 /* Vendor */ = { + isa = PBXGroup; + children = ( + 3B95201316944D7900BAF7F8 /* AFNetworking */, + ); + name = Vendor; sourceTree = ""; }; /* End PBXGroup section */ @@ -370,6 +378,7 @@ 3B95200F16944D5B00BAF7F8 /* Release */, ); defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; diff --git a/Twitter OSX Example Client/Twitter OSX Example Client/AppDelegate.m b/Twitter OSX Example Client/Twitter OSX Example Client/AppDelegate.m index a84f69d..77fa361 100644 --- a/Twitter OSX Example Client/Twitter OSX Example Client/AppDelegate.m +++ b/Twitter OSX Example Client/Twitter OSX Example Client/AppDelegate.m @@ -26,14 +26,12 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification LSSetDefaultHandlerForURLScheme((CFStringRef)@"af-twitter", (__bridge CFStringRef)[[NSBundle mainBundle] bundleIdentifier]); _twitterClient = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:@"https://api.twitter.com/"] key:@"4oFCF0AjP4PQDUaCh5RQ" secret:@"NxAihESVsdUXSUxtHrml2VBHA0xKofYKmmGS01KaSs"]; - + [_twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; + // Your application will be sent to the background until the user authenticates, and then the app will be brought back using the callback URL [_twitterClient authorizeUsingOAuthWithRequestTokenPath:@"oauth/request_token" userAuthorizationPath:@"oauth/authorize" callbackURL:[NSURL URLWithString:@"af-twitter://success"] accessTokenPath:@"oauth/access_token" accessMethod:@"POST" success:^(AFOAuth1Token *accessToken) { NSLog(@"Success: %@", accessToken); - NSLog(@"Your OAuth credentials are now set in the `Authorization` HTTP header"); - [_twitterClient setAuthorizationHeaderWithToken:accessToken.key]; - [_twitterClient registerHTTPOperationClass:[AFJSONRequestOperation class]]; [_twitterClient getPath:@"1/statuses/user_timeline.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { NSArray *responseArray = (NSArray *)responseObject; [responseArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {