Universal oAuth for objective-c class?
- by phpnerd211
I have an app that connects to 6+ social networks via APIs. What I want to do is transfer over my oAuth calls to call directly from the phone (not from the server). Here's what I have (for tumblr):
// Set some variables
NSString *consumerKey = CONSUMER_KEY_HERE;
NSString *sharedSecret = SHARED_SECRET_HERE;
NSString *callToURL = @"https://tumblr.com/oauth/access_token";
NSString *thePassword = PASSWORD_HERE;
NSString *theUsername = USERNAME_HERE;
// Calculate nonce & timestamp
NSString *nonce = [[NSString stringWithFormat:@"%d", arc4random()] retain];
time_t t;
time(&t);
mktime(gmtime(&t));
NSString *timestamp = [[NSString stringWithFormat:@"%d", (int)(((float)([[NSDate date] timeIntervalSince1970])) + 0.5)] retain];
// Generate signature
NSString *baseString = [NSString stringWithFormat:@"GET&%@&%@",[callToURL urlEncode],[[NSString stringWithFormat:@"oauth_consumer_key=%@&oauth_nonce=%@&oauth_signature_method=HMAC-SHA1&oauth_timestamp=%@&oauth_version=1.0&x_auth_mode=client_auth&x_auth_password=%@&x_auth_username=%@",consumerKey,nonce,timestamp,thePassword,theUsername] urlEncode]];
NSLog(@"baseString: %@",baseString);
const char *cKey = [sharedSecret cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [baseString cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
NSString *signature = [HMAC base64EncodedString];
NSString *theUrl = [NSString stringWithFormat:@"%@?oauth_consumer_key=%@&oauth_nonce=%@&oauth_signature=%@&oauth_signature_method=HMAC-SHA1&oauth_timestamp=%@&oauth_version=1.0&x_auth_mode=client_auth&x_auth_password=%@&x_auth_username=%@",callToURL,consumerKey,nonce,signature,timestamp,thePassword,theUsername];
From tumblr, I get this error: oauth_signature does not match expected value
I've done some forum scouring, and no oAuth for objective-c classes worked for what I want to do. I also don't want to have to download and implement 6+ social API classes into my project and do it that way.