Yahoo OAuth Error 401 Consumer Key Unknown - ios

I am trying to get a request token from Yahoo by making a POST request to the following URL:
https://api.login.yahoo.com/oauth/v2/get_request_token
I am making the request in the following way:
int randomNumber = rand();
NSDictionary *parameters = #{#"oauth_consumer_key": YahooConsumerKey,
#"oauth_signature_method": #"plaintext",
#"oauth_signature": YahooConsumerSecret,
#"oauth_version": #"1.0",
#"xoauth_lang_pref": #"en_us",
#"oauth_callback": #"oob",
#"oauth_timestamp": [self getTimeStamp],
#"oauth_nonce": [NSString stringWithFormat:#"%d", randomNumber]};
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:#"https://api.login.yahoo.com/oauth/v2/get_request_token" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
The problem is that I keep getting the error 401 with the explanation that the consumer key is unknown. I have pasted the consumer key from where I've created my project, so I really don't know what I am doing wrong. Any help will be appreciated. Thanks!

Your request looks good. Try appending #"%26" to your YahooConsumerSecret string. Here's your request link with the fix, it seems to be working!
When using plaintext as oauth_signature_method you need to append %26 at the end of your oauth_signature according to the Yahoo docs.

Related

How to intercept the response of AFNetworking request in Objective-C?

As titled, and how to check HTTP status of response?
For example, if the server returns http status code 403, I need to send a recall mail request again to take new access token.
Take a look at the below. In the failure block, retry/resend your query X number of times. Be sure to add logic to end retries at some point, so you don't end up with an infinite loop.
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager POST:url
parameters:object
success:^(AFHTTPRequestOperation *operation, id responseObject) {
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
if (operation.response.statusCode == 403) {
// retry
}
}
];
This answer may also be helpful.

Use AFNetworking to create a parse.com pointer object through REST API

I'm trying to use AFNetworking to call the parse.com REST API.
GET-Requests are working quite well so far. But when it comes to object creation I have a problem creating relations (pointers to object-ids) between two parse objects.
What I've tried so far:
NSString* parameterString = [NSString stringWithFormat:#"{\"__op\":\"AddRelation\",\"objects\":[{\"__type\":\"Pointer\",\"className\":\"MyClass\",\"objectId\":\"%#\"}]}", myClassObjectId];
NSDictionary *parameters = #{#"myClass": parameterString};
[self.manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { ... }];
Creating objects without pointers works well too. I think the error is related to my parameterString syntax:
Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: bad request (400)","code":111,"error":"invalid type for key myClass, expected *MyClass, but got string"
Any ideas? Thank you ;)
I got the answer, finally:
NSDictionary *params = #{#"myClass":#{#"__type":#"Pointer", #"className":#"MyClass", #"objectId":[foobar objectID]}};
[self.manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { ... }];
so... just plain JSON.

Remove escape from serialized NSSdictionary

I am pretty new to serializing with json, and I am facing a weird issue.
I am trying to send an NSURLRequest with a josn. The json is first stored into an NSSMutableDictionary and eventually is serialized. The serialized json object I get is escaped, meaning it has "\" in it all over the place.
The json is getting sent a server, but is getting denied. According to the admin the json is getting denied because its escaped. How can I removed all the back slashes from the serialized json before sending it.
HELP. I tried creating an NSString then converting to NSData then serialized and failed. I tried NSArray and failed. At least I think I did those correctly.
Did I make a mistake somewhere? is there a better way to do this?
Thanks,
Sam.
I came up with the same problem try AFNetworking
https://github.com/AFNetworking/AFNetworking
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:#"http://example.com/resources.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
NSlog(#"value->%#", [responseObject objectForKey#"json_key"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];

AFNetworking 2.0 Request w. Custom Header

Ive tried to avoid asking such a newb question on here, but im a Android dev learning IOS and I cant figure out for the life of me how to add a simple header to my post requests using AFNetworking 2.0. Below is my code so far which works if i want to make a request that doesnt require a header.Could anyone show me via adding to my snippet or providing a alternate one that does this? I came across this tut: http://www.raywenderlich.com/30445 that shows how to add a header under the "A RESTful Class" heading , but its for afnetworking 1.0 and is now depreciated as far as i can tell.
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = #{#"uid": #"1"};
AFHT
[manager POST:#"http://myface.com/api/profile/format/json" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(#"JSON: %#", responseObject);
self.feedArray = [responseObject objectForKey:#"feed"];
[self.tableView reloadData];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
} ];
}
Under AFHTTPRequestOperationManager you will notice a property called requestSerializer. This is of type AFHTTPRequestSerializer, and requests made through the HTTP manager are constructed with the headers specified by this object.
So, try this:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setValue:#"SomeValue" forHTTPHeaderField:#"SomeHeaderField"]
//Make your requests
You can read the headers dictionary from the request serializer as follows:
manager.requestSerializer.HTTPRequestHeaders
Note that once you set a header this way, it will be used for all other operations!
[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:#"staging" password:#"dustylendpa"];

AFHTTPRequestOperationManager urlstring encoding & Invalid parameter not satisfying: URLString

I'm loading json with afnetworking 2.0:
NSString *weatherUrl = #"http://www.souche.com/pages/xx/xx.json?request_message={\"type\":\"car-subdivision\"}";
weatherUrl = [weatherUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:weatherUrl parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(#"JSON: %#", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(#"Error: %#", error);
}];
console error:
'Invalid parameter not satisfying: URLString'
Is there need url encoding?
but i do it like this:
NSString *weatherUrl = #"http://www.souche.com/pages/dicAction/loadRootLevel.json?request_message={%22type%22:%22car-subdivision%22}";
still error!
How can i do?
It looks like there are a number of issues with the URL you're constructing, and the way you're passing (or not passing) parameters into AFNetworking. You don't need to construct your query string yourself, as AFNetworking will do that for you. As mentioned in my comment above, passing query=where UserName='abc' as part of a URL seems like a bad idea. However, here's a quick example of how you'd call AFNetworking's GET method if your URL was slightly different:
URL format:
https:////?username=abc&companyId=&page=1&pageSize=25&filterResultByColumns=true
you try this code :D

Resources