AFJSONRequestOperation create "oauth_problem=signature_invalid" - ios

I am developing app which require to use OAuth1.0 for call API.
I am able to Authenticate with OAuth1 and call GET method API.
But when I try to call POST method with passing JSON object. It give me "oauth_problem=signature_invalid"
Code for request :
NSMutableURLRequest *request = [self.twitterClient requestWithMethod:#"POST" path:apiURL parameters:jsonObj];
AFJSONRequestOperation *jsonOperation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Success: %#", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Error: %#", error);
}];
[jsonOperation start];
I am struggling with this. Any help will be appreciated. Thanks in advance.

I finally solve problem.
In my case problem was with signature_method
I set..
self.twitterClient.signatureMethod = AFPlainTextSignatureMethod;
before call request. And it works.

Related

validate self signed certificate wit AFNetworking

I want to connect my iOS App with AFNetwork to my webserver with a self-signed certificate. I found a solution on github (https://github.com/AFNetworking/AFNetworking/pull/694) I tried it out and the certificate pinning seems to work but i got an other error:
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed.
(NSURLErrorDomain error -1012.)" UserInfo=0x7bc2090 {NSErrorFailingURLKey=(my domain)}
Does anybody know whether this error has to do with the AFNetworking Framework and the self signed certificate or not?
Solved:
I found the solution for the error. I had to set the SSLPinningMode to AFSSLPinningModeCertificate now it works.
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"%#",error);
}];
operation.SSLPinningMode = AFSSLPinningModeCertificate;
[operation start];
Try this work around.
AFJSONRequestOperation *operation =[AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSDictionary *resDictionary = (NSDictionary *)JSON;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"%#",error);
}];
operation.securityPolicy.allowInvalidCertificates = YES;
[operation start];

How to add custom header to AFNetworking on a JSONRequestOperation

Hi, I have the following code accessing a URL:
NSString * stringURL = [NSString stringWithFormat:#"%#/%#/someAPI", kSERVICE_URL, kSERVICE_VERSION];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:stringURL]];
AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
completionHandler(JSON, nil);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
completionHandler(nil, error);
}];
But I want to pass the user token as a parameter on HEADER, like X-USER-TOKEN.
Cant find it on AFNetworking documentation, should I change the operation type?
Use AFHTTPClient or subclass it!
You can set default headers with -setDefaultHeader:value: like this :
[self setDefaultHeader:#"X-USER-TOKEN" value:userToken];
You can check the documentation
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setValue: #"X-USER-TOKEN" forHTTPHeaderField:#"< clientToken >"];
[AFJSONRequestOperation JSONRequestOperationWithRequest: request ...]
I did this :)
[manager.requestSerializer setValue:[NSString stringWithFormat:#"Token token=\"%#\"", _userObj.oAuth] forHTTPHeaderField:#"Authorization"];
If you have a layer of abstraction, let's say APIManager,
then you should do the following inside a particular method
[[HTTPClient sharedHTTPClient].requestSerializer setValue:YOUR_KEY forHTTPHeaderField:#"X-Register"];

AFNetworking Request API iOS

I'm doing a request to the server and the server returns a JSON. AFNetworking framework returns a wrong formatted JSON.
This is what the server sends:
{"email":"XXXXXXX","firstName":"XXXXXX","lastName":"XXXXXXX","gender":"male","userToken":"XXXXXXXXXXX"}
This is what AFNetworking receives:
{
email = "XXXXXXX";
firstName = XXXXXX;
gender = male;
lastName = XXXXXXX;
token = XXXXXXXXXXXX;
}
My code:
AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:server_ip]];
NSURLRequest *request = [client requestWithMethod:#"POST" path:path parameters:params];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"%#", JSON);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failed with Error: %#, %#", error, error.userInfo);
}];
[operation start];
The object you are printing out is the NSDictionary representation of the JSON received from the server.
If you want to see the raw JSON returned from the server, you should look at the responseString of the operation:
NSLog(#"%#", operation.responseString);

Error with AFNetworking for JSON Parsing

I have the following code for JSON Parsing:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json"]];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(#"Request Success %#",[JSON class]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(#"Request Failure Because %#",[error userInfo]);
}];
[operation start];
but I have Request Failure with the following error message:
NSErrorFailingURLKey = "https://www.dropbox.com/s/qz16qyi3julygl9/facebook.json";
NSLocalizedDescription = "Expected content type {(\n \"text/json\",\n \"application/json\",\n \"text/javascript\"\n)}, got text/html";
can somebody help me?
In my errorlog it prints "got text/html". So just add
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:#"text/html"]]
It works.
[AFJSONRequestOperation addAcceptableContentTypes:#"text/plain"]
The above is deprecated from AFNetworking 2.x. Instead you can call the following on the instance of the AFHTTPRequestOperation as follows
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:#"text/plain"];
Where manager is your instance of AFHTTPRequestOperation.
Source: https://github.com/AFNetworking/AFNetworking/issues/1381
Because the link you provide doesn't hotlink the file. It links to an HTML page to download the file. Try going there in a browser...
Try this link instead: https://dl.dropbox.com/s/qz16qyi3julygl9/facebook.json?dl=1 No guarantees it will work though. A lot of companies frown on directly linking to files in this way.

AFNetworking + AFJSONRequestOperation + Rails

I'm using NSMutableURLRequest before using AFJSONOperationRequest and I have a problems getting the data in my Rails app.
If I use :
NSMutableURLRequest *request = [[HTTPClient sharedClient] multipartFormRequestWithMethod:#"POST" path:path parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
{
}];
Then :
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
else {
}
}
I get my data correctly formatted in the Rails logs:
Parameters: {"contact"=>{"country_id"=>"45", "lastname"=>"Tutu"}}
But I don't need AFMultipartFormData to send a file... (no file to send)
So, instead, I use:
NSMutableURLRequest *request = [[HTTPClient sharedClient] multipartFormRequestWithMethod:#"POST" path:path parameters:dict];
with my AFJSONRequestOperation too. But, my parameters are now not set correctly in my rails app:
Parameters: {contact[country_id] => "45", contact[lastname] => "Tutu"}
instead of
Parameters: {"contact"=>{"country_id"=>"45", "lastname"=>"Tutu"}}
I don't understand why. It looks like the body of the request is not set correctly when I don't use the block: "constructingBodyWithBlock".
Don't use multipartFormRequestWithMethod.
Just use the postPath method like this
[[YourHTTPClient sharedClient] postPath:path
parameters:dict
success:^(AFHTTPRequestOperation *operation, NSString* path){
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
Edit:
If you need an operation use AFJSONRequestOperation directly
+ (AFJSONRequestOperation *)JSONRequestOperationWithRequest:(NSURLRequest *)urlRequest
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, id JSON))success
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON))failure;
NSURL *url = [NSURL URLWithString:#"http://yoursite.com/path"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
}];

Resources