Reddit API ios modhash / cookie问题

时间:2012-11-08 23:49:09

标签: objective-c ios xcode api reddit

我试图通过我的iOS应用程序提交到Reddit。我可以正常登录并发回一个modhash和一个cookie,我通过NSUserDefaults保存。

问题是当我发布数据时,我一直收到“USER_REQUIRED”作为响应,即使我已经在帖子中包含了modhash并设置了我的会话cookie。我甚至在我的应用代表中包含了以下内容:

[[NSHTTPCookieStorage sharedHTTPCookieStorage]
    setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];

但它仍然无效。这是我的代码:

-(void) post {

    NSString *modhash2 = [[NSUserDefaults standardUserDefaults]  
        objectForKey:@"modhash"];

    NSString *urlString = [NSString 
        stringWithFormat:@"https://ssl.reddit.com/api/submit"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSString *contentType = [NSString stringWithFormat:@"application/x-www-form-urlencoded;"];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    [request addValue:redditCookie forHTTPHeaderField:@"Cookie"];


    [request setHTTPMethod:@"POST"];
    NSString *httpBody = [NSString stringWithFormat
        :@" ?uh=%@&kind=link&url=%@&sr=%@&title=%@&r=%@&api_type=json",
        modhash2,
        @"www.google.com",
        @"test",
        @"Google.com",
        @"test"];


    [request setHTTPBody:[httpBody dataUsingEncoding:NSASCIIStringEncoding]];

    NSURLResponse* response;
    NSError* error = nil;
    NSData* result = [NSURLConnection
        sendSynchronousRequest:request
        returningResponse:&response
        error:&error];

    NSDictionary *json = [NSJSONSerialization
        JSONObjectWithData:result
        options:NSJSONReadingMutableContainers
        error:nil];

    NSDictionary *responseJSON = [json valueForKey:@"json"];
    NSLog(@"RETURN: %@",responseJSON);
}

有什么想法吗?

0 个答案:

没有答案