1

I am having a small Reddit API problem here. I am trying to follow the reddit api, as outlined here:

https://github.com/reddit/reddit/wiki/API

Logging in using a simple NSMutableURLRequest is not a problem:

NSString *user = [[NSString alloc]initWithString:[userFld text]];
NSString *passwd = [[NSString alloc]initWithString:[passwordFld text]];
NSString *urlString = [NSString stringWithFormat:@"http://www.reddit.com/api/login/username/?user=%@&passwd=%@", 
                       [user stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
                       [passwd stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

Which gives me a result from which I extract the user mod-hash:

4029916%2C2010-04-30T22%3A51%3A52%2C1243925043100000000000000000000000000000

Next I am trying to post, using:

NSString *redditUrlString = [NSString stringWithFormat:
    @"http://www.reddit.com/api/submit/?uh=%@&kind=link&url=%@&sr=%@&title=%@&r=%@&api_type=json",
    [appDelegate globalUhString],
    @"www.google.com",
    @"funny",
    @"Google.com",
    @"funny"];

NSURL *url = [NSURL URLWithString:redditUrlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

However, when I extract the JSON response form the data received from this connection, I always receive the error below no matter what I use as the mod-hash:

[".error.USER_REQUIRED"]

Can someone explain what I have done incorrectly/how I can fix it?

LU RD
  • 34,438
  • 5
  • 88
  • 296

1 Answers1

1

It looks like you've not passing in the reddit_session cookie along with the uh parameter. The api documentation seems to explain the reason for receiving the error. According to this answer, it looks like you may need to issue the code:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

in the applicationDidBecomeActive handler, as it may be rejecting the cookies due to the CookieAcceptPolicy being shared amongst all applications.

Community
  • 1
  • 1
Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • So I have added this line to the above method, but how does one capture the cookie from the NSMutableURLRequest? Might it be some code in the `(connection *)connection didReceiveData` method? – steve sullivan Mar 17 '12 at 22:16
  • It is supposed to store it automatically. From the docs: The NSHTTPCookie class encapsulates a cookie, providing accessors for many of the common cookie attributes. It also provides methods to convert HTTP cookie headers to NSHTTPCookie instances and convert an NSHTTPCookie instance to headers suitable for use with an NSURLRequest. The URL loading system automatically sends any stored cookies appropriate for an NSURLRequest. unless the request specifies not to send cookies. Likewise, cookies returned in an NSURLResponse are accepted in accordance with the current cookie acceptance policy. – Anya Shenanigans Mar 17 '12 at 22:21
  • This solved the issue. Once I got the cookie from logging in, I was able to set the cookie in the new request. – steve sullivan Mar 29 '12 at 02:04