I’ve been trying to get a basic OAuth interaction working without success. There are certainly similar questions already posed on SO, but most of them have no replies. I’m getting pretty desperate here, so I’m just going to start by posting my entire code:
// OAuth parameters
NSString *oauthNonce = [self genRandStringLength:20];
NSString *oauthSignatureMethod = [NSString stringWithFormat:@"HMAC-SHA1"];
time_t oauthTimeStamp = (time_t) [[NSDate date] timeIntervalSince1970];
// generate OAuth signature
NSString *encodedUrlString = [self urlEncode:urlString];
NSString *oauthParameters = [NSString stringWithFormat:@"oauth_consumer_key=%@",oauthConsumerKey];
oauthParameters = [oauthParameters stringByAppendingFormat:@"&oauth_nonce=%@",oauthNonce];
oauthParameters = [oauthParameters stringByAppendingFormat:@"&oauth_signature_method=%@",oauthSignatureMethod];
oauthParameters = [oauthParameters stringByAppendingFormat:@"&oauth_timestamp=%d",oauthTimeStamp];
oauthParameters = [oauthParameters stringByAppendingFormat:@"&oauth_token=%@",oauthAccessToken];
oauthParameters = [oauthParameters stringByAppendingFormat:@"&oauth_version=1.0"];
NSString *oauthEncodedParameters = [self urlEncode:oauthParameters];
NSString *oauthBaseString = [NSString stringWithFormat:@"GET&%@&%@",encodedUrlString,oauthEncodedParameters];
NSString *oauthKey = [NSString stringWithFormat:@"%@&%@", oauthConsumerSecret, oauthAccessTokenSecret];
NSString *oauthEncodedKey = [self urlEncode:oauthKey];
OAHMAC_SHA1SignatureProvider *provider = [[OAHMAC_SHA1SignatureProvider alloc] init];
NSString *oauthSignature = [provider signClearText:oauthBaseString withSecret:oauthEncodedKey];
NSString *oauthEncodedSignature = [self urlEncode:oauthSignature];
// prepare request
NSString *oauthHeader = @"OAuth ";
oauthHeader = [oauthHeader stringByAppendingFormat:@"oauth_consumer_key=\"%@\"",oauthConsumerKey];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_token=\"%@\"",oauthAccessToken];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature_method=\"%@\"",oauthSignatureMethod];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature=\"%@\"",oauthEncodedSignature];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_timestamp=\"%d\"",oauthTimeStamp];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_nonce=\"%@\"",oauthNonce];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_version=\"1.0\""];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
[request setValue:oauthHeader forHTTPHeaderField:@"Authorization"];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
When I look at the http response code I get a 500. I’ve reached out to the service provider regarding the unexpected 500 response.
In the meanwhile, does anything stand out to you on what I’m possibly doing wrong?
Even just pointing me to some working sample code would be highly appreciated. I’ve read a lot of stuff out there, but I’m obviously getting some nuance wrong in obj-c. Thanks in advance.
If I were you I would check out the source code of ShareKit on github, which integrates with many OAuth web services. I believe it uses OAuthConsumer which may be of particular use to you. OAuthConsumer has some documentation and you might want to try integrating it into your application.
If you take a look at OAMutableURLRequest, you can see how OAuthConsumer creates requests. I don’t see any big differences in the way your headers are setup, except that they have a
realmheader. My suggestion would be to try and use OAuthConsumer to make the request and see if it works. If it doesn’t work with OAuthConsumer then I would imagine the problem lays elsewhere (maybe on the server?).