Google client-login in Objective-C
I am trying to write a code for google client-login in objective-c. I get Auth with email and password from "https://www.google.com/accounts/ClientLogin" and I can successfully login google by POSTER (firefox).
When I write the code for objective-c, however I cannot login and get error code 401.
Could someone help me what I am doing wrong? Here is my code.
// 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."); }
Answers
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"];