Reputation: 238
I am making an application that requires oAuth 1.0 authentication. I have access to consumer key and consumer secret given by the client. I have tried to do it with AFNetworking but it has not come up well. Could someone suggest me a good tutorial for the same. Actually I am getting an error for unauthenticated user.
Upvotes: 0
Views: 1153
Reputation: 238
I found the answer to this question. First of all I used AFNetworking and AFOauth1Client for the same.
AFOAuth1Client * client = [[AFOAuth1Client alloc] initWithBaseURL:[NSURL URLWithString:<your URL>] key:<Your Consumer Key> secret:<Your Consumer Secret>];
[client setOauthAccessMethod:@"POST"];
[client setSignatureMethod:AFHMACSHA1SignatureMethod];
[client setDefaultHeader:@"Content-Type" value:@"application/x-www-form-urlencoded"];
Next I created a request for this client and set the Body of the request
NSMutableURLRequest * request =[client requestWithMethod:@"POST" path:<URL Path> parameters:nil];
//here path is actually the name of the method
[request setHTTPBody:[<Your String Data> dataUsingEncoding:NSUTF8StringEncoding]];
Then I made use of AFHttpRequestOperation with the request made in the upper step
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[client registerHTTPOperationClass:[AFHTTPRequestOperation class]];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
// Success Print the response body in text
NSLog(@"Response: %@", [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]);
//parsing the xml Response
NSXMLParser * parser= [[NSXMLParser alloc] initWithData:responseObject];
[parser setDelegate:self];
[parser parse];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//Something Went wrong..
NSLog(@"Error: %@", error);
}];
[operation start];
Upvotes: 3