0
我在這裏有一個小的Reddit API問題。我試圖按照reddit的API,如概述這裏:error.USER_REQUIRED當試圖提交Reddit鏈接在objective-c
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哈希:
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哈希使用:
[」 .error .USER_REQUIRED「]
有人可以解釋我做了什麼不正確/我該如何解決它?
所以我已經將此行添加到上述方法,但是如何從NSMutableURLRequest捕獲cookie?可能它是'(connection *)連接didReceiveData'方法中的一些代碼? – 2012-03-17 22:16:35
它應該自動存儲它。從文檔:NSHTTPCookie類封裝了一個cookie,爲許多常用的cookie屬性提供訪問器。它還提供了將HTTP cookie標頭轉換爲NSHTTPCookie實例並將NSHTTPCookie實例轉換爲適用於NSURLRequest的標頭的方法。 URL加載系統自動發送適合NSURLRequest的任何存儲的cookie。除非請求指定不發送cookie。同樣,根據當前的Cookie接受策略,接受在NSURLResponse中返回的Cookie。 – Petesh 2012-03-17 22:21:33
這解決了這個問題。一旦我登錄了cookie,我就可以在新的請求中設置cookie。 – 2012-03-29 02:04:36