0

I have a problem with setting the HTTPBody of a request correctly. I'm using Oauth to communicate with our API server, the only difference is, that I need to send the data through body, not the header (using OauthConsumer.framework - http://code.google.com/p/oauthconsumer/wiki/UsingOAuthConsumer).

This is my code:

NSString *oauthHeader = [NSString stringWithFormat:@"OAuth realm=\"%@\", oauth_consumer_key=\"%@\", %@oauth_signature_method=\"%@\", oauth_signature=\"%@\", oauth_timestamp=\"%@\", oauth_nonce=\"%@\", oauth_version=\"1.0\"%@",
                         [realm URLEncodedString],
                         [consumer.key URLEncodedString],
                         oauthToken,
                         [[signatureProvider name] URLEncodedString],
                         [signature URLEncodedString],
                         timestamp,
                         nonce,
                         extraParameters];

//[self setValue:oauthHeader forHTTPHeaderField:@"Authorization"];
NSLog(@"%@", oauthHeader);
[self setHTTPBody:[oauthHeader dataUsingEncoding:NSASCIIStringEncoding]];

This is the output of NSlog.

OAuth realm="", oauth_consumer_key="key", oauth_signature_method="HMAC-SHA1", oauth_signature="HT2UwJoW4dSNh1gXkAzQThLp0Sk%3D", oauth_timestamp="1340789377", oauth_nonce="3BDF0A1A-4FB0-40EF-95EA-5CB8B0FD07C1", oauth_version="1.0"

And this is what server reads, why the array is wrong? It's not server side bug, our C# client does it without any problem.

array(1) {
  ["OAuth_realm"]=>
  string(215) """, oauth_consumer_key="key", oauth_signature_method="HMAC-SHA1", oauth_signature="HT2UwJoW4dSNh1gXkAzQThLp0Sk=", oauth_timestamp="1340789377", oauth_nonce="3BDF0A1A-4FB0-40EF-95EA-5CB8B0FD07C1", oauth_version="1.0""
}
Thunder
  • 611
  • 1
  • 8
  • 25
  • This is what server receives from the Windows client:
    array(7) {  ["app_status"]=>  string(67) "{"downloads":[],"application":{"status":"Idle","transfer_speed":0}}"  ["oauth_consumer_key"]=>  string(40) "1e98d7a0b09c4a155b78cdb5ca09bee309355bb7"  ["oauth_nonce"]=>  string(8) "91942949"  ["oauth_signature_method"]=>  string(9) "HMAC-SHA1"  ["oauth_timestamp"]=>  string(10) "1340717947"  ["oauth_version"]=>  string(3) "1.0"  ["oauth_signature"]=>  string(28) "Q1Ac4eF690stWrvaOCi0knxThjY="}
    – Thunder Jun 27 '12 at 10:37

3 Answers3

1

The problem seems to be that you do not separate the different key-value-pairs from each other. This is simply done by concatenating them with "&" (without quotes). You set a comma between the key-value associations.
In addition you don't need to encapsulate the values within quotes unless you want these quotes to be a part of the value on receivers side.

Your resulting data string should then look like this:

NSString *oauthHeader = [NSString stringWithFormat:@"OAuth_realm=%@&oauth_consumer_key=%@&%@oauth_signature_method=%@&oauth_signature=%@&oauth_timestamp=%@&oauth_nonce=%@&oauth_version=1.0%@",
                     [realm URLEncodedString],
                     [consumer.key URLEncodedString],
                     oauthToken,
                     [[signatureProvider name] URLEncodedString],
                     [signature URLEncodedString],
                     timestamp,
                     nonce,
                     extraParameters];

Keep in mind that your extraparameters need to be separated by "&" too.

TRD
  • 1,027
  • 11
  • 20
0

Looks like the oauth_signature is not properly encoded (the percent sign is still there). Instead of [signature URLEncodedString] try this:

[signature stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

and see if it makes any difference.

lawicko
  • 7,246
  • 3
  • 37
  • 49
  • No, that's not it. The problem is that the array which server receives is with 1 object, but it should be array(7). OAuth_realm is treated as the key for the first element, but its value is the rest of the received string. – Thunder Jun 27 '12 at 10:32
0

Ok, I was stupid enough to not notice that our API uses "&" instead of "," to separate keys from each other. Problem solved.

Thunder
  • 611
  • 1
  • 8
  • 25