3

我能够成功获取我的 Google Reader 帐户的 SID (SessionID)。为了在 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];

作为 Google 的响应,我收到 403 Forbidden 错误。我可能做得不对。我根据 NSHTTPCookie 的文档设置字典值。

4

1 回答 1

1

cookie 属性的键应该是NSHTTPCookie常量,而不是你给它们的文字字符串。

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

请注意,值常量不等于它们的名称;一般来说,它们似乎是没有“NSHTTPCookie”的名称(例如NSHTTPCookieDomain == @"Domain"),但您不应该依赖这个。重新阅读文档。

于 2010-05-05T01:05:45.997 回答