1

我在这里遇到了一个小的 Reddit API 问题。我正在尝试遵循 reddit api,如此处所述:

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

使用简单的 NSMutableURLRequest 登录不是问题:

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];

这给了我一个结果,我从中提取了用户 mod-hash:

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

接下来我尝试发布,使用:

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];

但是,当我从从此连接接收的数据中提取 JSON 响应时,无论我使用什么作为 mod-hash ,我总是会收到以下错误:

[".error.USER_REQUIRED"]

有人可以解释我做错了什么/我该如何解决?

4

1 回答 1

1

看起来您没有将reddit_sessioncookie 与uh参数一起传递。api文档似乎解释了收到错误的原因。根据this answer,您可能需要发布代码:

[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

在处理程序中,因为它可能会由于在所有应用程序之间共享applicationDidBecomeActive而拒绝 cookie 。CookieAcceptPolicy

于 2012-03-17T21:41:23.883 回答