Google Reader API 与 Objective-C - 获取令牌时出现问题
我能够成功获取我的 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 的文档设置字典值。
I am able to successfully get the SID (SessionID) for my Google Reader account. In order to obtain the feed and do other operations inside Google Reader, you have to obtain an authorization token. I'm having trouble doing this. Can someone shed some light?
//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];
I get a 403 Forbidden error as the response from Google. I'm probably not doing it right. I set the dictionary values according to the documentation for NSHTTPCookie.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
cookie 属性的键应该是
NSHTTPCookie
常量,而不是您提供的文字字符串。请注意,值常量不等于它们的名称;一般来说,它们似乎是不带“NSHTTPCookie”的名称(例如
NSHTTPCookieDomain == @"Domain"
),但您不应该依赖于此。重新阅读文档。The keys of a cookie properties should be the
NSHTTPCookie
constants, not literal strings as you give them.Note that the values constants aren't equal to their names; generally speaking, they appear to be the name without the "NSHTTPCookie" (e.g.
NSHTTPCookieDomain == @"Domain"
), but you shouldn't rely on this. Re-read the documentation.