Making a GET Call to an OAuth Protected URL, Part 2

I am trying to get the basic OAuth interaction working without success. Of course, there are similar questions already posed on SO, but most of them have no answers. I get desperation here, so I just start by posting all the 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 500. I contacted the service provider regarding an unexpected 500 response.

Meanwhile, does something stand out to you that I may not be right?

Even just pointing to some working sample code, we greatly appreciate it. I read a lot of things there, but I obviously get some nuance wrong in obj-c. Thanks in advance.

+4
source share
4 answers

If I were you, I would check the source code of ShareKit on github , which integrates with many OAuth web services. I believe that it uses OAuthConsumer , which can be especially useful for you. OAuthConsumer has documentation and you can try integrating it into your application.

If you look at OAMutableURLRequest , you will see how OAuthConsumer creates requests. I do not see much difference in the settings of your headers, except that they have a realm header. My suggestion would be to try using OAuthConsumer to query and see if it works. If it does not work with OAuthConsumer, then I would suggest that the problem lies elsewhere (perhaps on the server?).

+4
source

It has been a while since I played with OAuth, but I did a small project for PHP and Objective-C that removed ALL of my needs, had ever bothered with it again.

I assume that your header fields should be ordered by field name, so the order should look something like this:

 NSString *oauthHeader = @"OAuth "; oauthHeader = [oauthHeader stringByAppendingFormat:@"oauth_consumer_key=\"%@\"",oauthConsumerKey]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_nonce=\"%@\"",oauthNonce]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_timestamp=\"%d\"",oauthTimeStamp]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_token=\"%@\"",oauthAccessToken]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature=\"%@\"",oauthEncodedSignature]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature_method=\"%@\"",oauthSignatureMethod]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_version=\"1.0\""]; 

Please feel free to check out my uOAuth (micro-OAuth) project in Gitorious for Objective-C (there is also a PHP version there if you want to take a look too).

If you need more clarification, I will gladly edit the question or answer to your comment.

Kudos!

+1
source

I had a problem with the header fields, so I got rid of them by including all the parameters in the url encoding. I did the following.

  • Using the OAuthConsumer-iphone twitter app http://code.google.com/p/oauthconsumer-iphone/
  • Ready (void) to prepare for OADataFetcher.m, get rid of oauthheader and include all parameters in url parameters by executing [request setParameters: [(NSArray *) allParameters urlEncoded]];
  • Rest should be simple.
  • Thus, all the files that you will concentrate are oauthTwitterAppViewController, OAMutableURLRequest, OADataFetcher, AuthorizeWebViewController.

It took me almost a week to synchronize with the service provider for this to work. But if you have all the sources ready, you may need 2 days.

+1
source

I saw your code and after looking at some other questions on this website, I finally create the executable code. In my case, I use AFHTTPClient to initiate the request, I just use OAMutableURLRequest to create the authorization header, then I use the authorization header to join the request that I send to the server.

I am sharing my code here for those who need it.

In your case, I think you should check sending the request to the server and use Fiddler to compose a test request to debug your service.

Sorry for my bad english: p.

 NSDictionary * jsonDic; __block NSString *responsebody = @""; OAConsumer *consumer = [[OAConsumer alloc] initWithKey:@"123" secret:@"456"]; NSURL *url = [NSURL URLWithString:@"http://MyServiceURL/MyServiceFunction"]; OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url consumer:consumer token:nil realm:nil signatureProvider:nil]; [request prepare]; NSString *oauthHeader = @"OAuth "; oauthHeader = [oauthHeader stringByAppendingFormat:@"oauth_consumer_key=\"%@\"",@"123"]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_token=\"%@\"",@""]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature_method=\"%@\"",@"HMAC-SHA1"]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_signature=\"%@\"",encodeToPercentEscapeString(request.signature)]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_timestamp=\"%ld\"", (time_t) [[NSDate date] timeIntervalSince1970]]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_nonce=\"%@\"",request.nonce]; oauthHeader = [oauthHeader stringByAppendingFormat:@",oauth_version=\"1.0\""]; //NSLog(@"oauthHeader: %@", oauthHeader); [AFNetworkActivityIndicatorManager sharedManager].enabled = YES; NSURL *baseURL = [NSURL URLWithString:@"http://MyServiceURL"]; AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; [client setParameterEncoding:AFJSONParameterEncoding]; [client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; [client setDefaultHeader:@"Content-Type" value:@"application/json"]; [client setDefaultHeader:@"Authorization" value:oauthHeader]; NSDictionary *finalDict = @{@"user":@{@"id":@"001"}}; // create a JSON string from your NSDictionary NSError *error; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalDict options:NSJSONWritingPrettyPrinted error:&error]; [client postPath:@"/MyServiceFunction" parameters:finalDict success:^(AFHTTPRequestOperation *operation, id JSON) { responsebody = operation.responseString; jsonDic = [NSJSONSerialization JSONObjectWithData:[responsebody dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:nil]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"Hit error: %@", error); }]; 

and

 NSString* encodeToPercentEscapeString(NSString *string) { return (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef) string, NULL, (CFStringRef) @"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));} 
0
source

All Articles