Skip to content
Browse files

Merge pull request #4 from znq/master

Support for Twitter's "POST statuses/update_with_media"
  • Loading branch information...
2 parents 4472389 + bd90b51 commit 2828d9215ef33591c0852adcde1f11e3a6262849 @rsieiro committed Mar 21, 2012
Showing with 24 additions and 17 deletions.
  1. +2 −1 RSOAuthEngine/RSOAuthEngine.h
  2. +22 −16 RSOAuthEngine/RSOAuthEngine.m
View
3 RSOAuthEngine/RSOAuthEngine.h
@@ -75,7 +75,8 @@ typedef enum _RSOOAuthSignatureMethod {
- (NSString *)customValueForKey:(NSString *)key;
- (void)fillTokenWithResponseBody:(NSString *)body type:(RSOAuthTokenType)tokenType;
- (void)setAccessToken:(NSString *)token secret:(NSString *)tokenSecret;
-- (void)signRequest:(MKNetworkOperation *)request;
+- (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth;
- (void)enqueueSignedOperation:(MKNetworkOperation *)op;
+- (void)enqueueSignedOperation:(MKNetworkOperation *)op signOnlyWithOAuthParams:(BOOL)onlyOAuth;
@end
View
38 RSOAuthEngine/RSOAuthEngine.m
@@ -38,7 +38,7 @@
@interface RSOAuthEngine ()
-- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request;
+- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth;
- (NSString *)generatePlaintextSignatureFor:(NSString *)baseString;
- (NSString *)generateHMAC_SHA1SignatureFor:(NSString *)baseString;
- (void)addCustomValue:(NSString *)value withKey:(NSString *)key;
@@ -136,28 +136,30 @@ - (id)initWithHostName:(NSString *)hostName
#pragma mark - OAuth Signature Generators
-- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request
+- (NSString *)signatureBaseStringForRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth
{
NSMutableArray *parameters = [NSMutableArray array];
- NSURL *url = [NSURL URLWithString:request.url];
-
+
// Get the base URL String (with no parameters)
NSArray *urlParts = [request.url componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"?#"]];
NSString *baseURL = [urlParts objectAtIndex:0];
// Add parameters from the query string
- NSArray *pairs = [url.query componentsSeparatedByString:@"&"];
- [pairs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- NSArray *elements = [obj componentsSeparatedByString:@"="];
- NSString *key = [[elements objectAtIndex:0] urlEncodedString];
- NSString *value = (elements.count > 1) ? [[elements objectAtIndex:1] urlEncodedString] : @"";
+ if (!onlyOAuth) {
+ NSURL *url = [NSURL URLWithString:request.url];
+ NSArray *pairs = [url.query componentsSeparatedByString:@"&"];
+ [pairs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
+ NSArray *elements = [obj componentsSeparatedByString:@"="];
+ NSString *key = [[elements objectAtIndex:0] urlEncodedString];
+ NSString *value = (elements.count > 1) ? [[elements objectAtIndex:1] urlEncodedString] : @"";
- [parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"key", value, @"value", nil]];
- }];
+ [parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:key, @"key", value, @"value", nil]];
+ }];
+ }
// Add parameters from the request body
// Only if we're POSTing, GET parameters were already added
- if ([[[request HTTPMethod] uppercaseString] isEqualToString:@"POST"]) {
+ if (!onlyOAuth && [[[request HTTPMethod] uppercaseString] isEqualToString:@"POST"]) {
[request.readonlyPostDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[parameters addObject:[NSDictionary dictionaryWithObjectsAndKeys:[key urlEncodedString], @"key", [obj urlEncodedString], @"value", nil]];
}];
@@ -313,7 +315,7 @@ - (void)setAccessToken:(NSString *)token secret:(NSString *)tokenSecret
[self setOAuthValue:nil forKey:@"oauth_verifier"];
}
-- (void)signRequest:(MKNetworkOperation *)request
+- (void)signRequest:(MKNetworkOperation *)request signOnlyWithOAuthParams:(BOOL)onlyOAuth
{
NSAssert(_oAuthValues && self.consumerKey && self.consumerSecret, @"Please use an initializer with Consumer Key and Consumer Secret.");
@@ -322,7 +324,7 @@ - (void)signRequest:(MKNetworkOperation *)request
[self setOAuthValue:[NSString uniqueString] forKey:@"oauth_nonce"];
// Construct the signature base string
- NSString *baseString = [self signatureBaseStringForRequest:request];
+ NSString *baseString = [self signatureBaseStringForRequest:request signOnlyWithOAuthParams:(BOOL)onlyOAuth];
// Generate the signature
switch (_signatureMethod) {
@@ -351,10 +353,14 @@ - (void)signRequest:(MKNetworkOperation *)request
[request addHeaders:oauthHeader];
}
-- (void)enqueueSignedOperation:(MKNetworkOperation *)op
+- (void)enqueueSignedOperation:(MKNetworkOperation *)op {
+ [self enqueueSignedOperation:op signOnlyWithOAuthParams:NO];
+}
+
+- (void)enqueueSignedOperation:(MKNetworkOperation *)op signOnlyWithOAuthParams:(BOOL)onlyOAuth
{
// Sign and Enqueue the operation
- [self signRequest:op];
+ [self signRequest:op signOnlyWithOAuthParams:onlyOAuth];
[self enqueueOperation:op];
}

0 comments on commit 2828d92

Please sign in to comment.
Something went wrong with that request. Please try again.