我能够成功地获得我的谷歌阅读器帐户的sid(sessionid)。为了获得feed并在google reader中执行其他操作,您必须获得一个授权令牌。我做这件事有困难。有人能发光吗?

//Create a cookie to append to the GET request
NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SID",@"NSHTTPCookieName",self.sessionID,@"NSHTTPCookieValue",@".google.com",@"NSHTTPCookieDomain",@"/",@"NSHTTPCookiePath",nil];
NSHTTPCookie *authCookie = [NSHTTPCookie cookieWithProperties:cookieDictionary];

//The URL to obtain the Token from
NSURL *tokenURL = [NSURL URLWithString:@"http://www.google.com/reader/api/0/token"];
NSMutableURLRequest *tokenRequest = [NSMutableURLRequest requestWithURL:tokenURL];

//Not sure if this is right: add cookie to array, extract the headers from the cookie inside the array...?
[tokenRequest setAllHTTPHeaderFields:[NSHTTPCookie requestHeaderFieldsWithCookies:[NSArray arrayWithObjects:authCookie,nil]]];

//This gives me an Error 403 Forbidden in the didReceiveResponse of the delegate
[NSURLConnection connectionWithRequest:tokenRequest delegate:self];

我得到了一个403禁止错误作为谷歌的回应。我可能做得不对。我根据nshttpcookie的文档设置字典值。

最佳答案

Cookie属性的键应该是NSHTTPCookie constants,而不是给它们的文字字符串。

NSDictionary *cookieDictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"SID",NSHTTPCookieName,
          self.sessionID,NSHTTPCookieValue,
          @".google.com",NSHTTPCookieDomain,
          @"/",NSHTTPCookiePath,
          nil];

注意,值常量不等于它们的名称;一般来说,它们看起来是没有“NSHTTCPKOKE”(例如NSHTTPCookieDomain == @"Domain")的名称,但不应依赖于此。重新阅读文档。

关于iphone - 带有Objective-C的Google Reader API-获取 token 时出现问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2769888/

10-10 20:49