0

I'm trying to get an image sitting at a URL that is protected by OAuth. I've looked through the spec docs but can't seem to get everything right. Here's what I'm doing:

Construct an OAuth Authorization header:

NSString *oauthHeader = @"Authorization: OAuth ";
oauthHeader = [oauthHeader stringByAppendingFormat:@"oauth_consumer_key=%@",oauthConsumerKey];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_token=%@",oauthAccessToken];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature_method=HMAC-SHA1"];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature=%@",escapedString];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_timestamp=%d",oauthTimeStamp];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_nonce=%@",oauthNonce];
oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_version=1.0"];

Add the Authorization to the http request:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageUrl]];
[request setValue:oauthHeader forHTTPHeaderField:@"Authorization"];

And then send it on.

I get an empty response back with a 404 code. I am certain the URL is correct, because as a test if I disable the OAuth protection on the URL I am able to retrieve it fine.

Does anything stick out to you as me doing something wrong? Thanks much.

Steven
  • 1,049
  • 2
  • 14
  • 32

1 Answers1

1

All OAuth parameters look good, but your code is far from being complete. A wild guess would be that the service provider does not understand the Authorization HTTP header or that you failed to create a valid OAuth signature (in that case try maybe PLAINTEXT).

In each case: replying with a 404 is definitely an error and you should contact the OAuth provider, giving as much detail as you can.

Jan Ahrens
  • 26
  • 3
  • You're right, my signature method at the moment is generated as if signature method were PLAINTEXT (`escapedString`, whose construction code is not included in my post). I'm working on getting the HMAC-SHA1 encoding, which is more difficult than I expected. Any sample code in iOS you can point me to? Thanks. – Steven Jun 16 '12 at 15:36
  • I don't have any sample code, but I highly recommend against writing the OAuth signature code yourself. There are excellent OAuth libraries for most programming languages available. – Jan Ahrens Jun 16 '12 at 15:49