1

我正在尝试在objective-c中为google客户端登录编写代码。我从“https://www.google.com/accounts/ClientLogin”获得带有电子邮件和密码的身份验证,并且我可以通过 POSTER (firefox) 成功登录谷歌。

当我为objective-c编写代码时,我无法登录并得到错误代码401。

有人可以帮我做错什么吗?这是我的代码。

// URL to check user info
NSURL *url = [NSURL URLWithString:@"http://www.google.com/reader/api/0/user-info"];

// authorization 
NSString *auth = authString;

// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

// add auth information in header
auth = [NSString stringWithFormat:@"GoogleLogin auth=%@", auth];
[request addValue:auth forHTTPHeaderField:@"Authorization"];

// send request
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
    NSLog(@"request send.");
} else {
    NSLog(@"Connection Failed when getting feeds.");
}
4

1 回答 1

0

I have managed to log-in with Objective-C. I found my Header "Authentication" was null even though I set it in the code. I needed to remove %0A (in ASCII) to set the cookie.

Here is the modification I made. (Hope it would be helpful for someone)

// Encode ASCII
NSString * authEncoded = [auth stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

// Remove %0A
authEncoded =  [authEncoded stringByReplacingOccurrencesOfString:@"%0A" withString:@""];

// Create value for header
authEncoded = [[NSString alloc]initWithFormat:@"GoogleLogin auth=%@", authEncoded];

I used "authEncode" to set a header and everything works fine so far.

[request addValue:auth forHTTPHeaderField:@"Authorization"];
于 2012-09-23T00:52:04.980 回答