我在之前的stackoverflow.com帖子中遇到了同样的问题.
具体来说,我似乎能够正确获取"Auth"令牌,但是当我访问后面的页面时,尝试在标题中使用它仍然只返回登录页面的HTML.
以下是与此帖相关的链接,我已确定您需要对此URL进行后续调用.
然后到URL调用会给你一个ACSID的cookie,然后需要在后续调用传递,以维持认证状态.
当请求此cookie,我读过各个岗位说你需要通过将其追加到查询字符串,从而指定你原来的身份验证令牌:
?auth=this_is_my_token
我也看到了在描述你应该设置在HTTP头谷歌的文档,这样一个HTTP标头名称/值是:
Authorization: GoogleLogin auth=yourAuthToken
我尝试了两种方法,但没有看到任何cookie返回.我使用Wireshark的,为的LiveHTTPHeaders Firefox和简单的NSLog语句尝试看看如果返回这样的事.
以下是我一直在使用的代码段.
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://yourapp.appspot.com/_ah/login?auth=%@", [token objectForKey:@"Auth"]]]; NSHTTPURLResponse* response; NSError* error; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setValue:[NSString stringWithFormat:@"GoogleLogin auth=%@", [token objectForKey:@"Auth"]] forHTTPHeaderField:@"Authorization"]; NSData * data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; //show me all header fields NSLog([[response allHeaderFields] description]); //show me the response NSLog(@"%@", [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease]); NSArray * all = [NSHTTPCookie cookiesWithResponseHeaderFields:[response allHeaderFields] forURL:[NSURL URLWithString:@"http://yourapp.appspot.com/_ah/login"]]; //show me all cookies for (NSHTTPCookie *cookie in all) { NSLog(@"Name: %@ : Value: %@", cookie.name, cookie.value); }
我希望您可以使用ClientLogin
Google App Engine代码.
在此问题中添加示例代码,因为有人直接就我的解决方案与我联系.请注意,必须在初始令牌请求中将"service"参数设置为"ah".
令牌的初始请求[同步完成]注意:"service"参数设置为"ah","source"设置为"myapp",您应该使用您的应用程序名称.
//create request NSString* content = [NSString stringWithFormat:@"accountType=HOSTED_OR_GOOGLE&Email=%@&Passwd=%@&service=ah&source=myapp", [loginView username].text, [loginView password].text]; NSURL* authUrl = [NSURL URLWithString:@"https://www.google.com/accounts/ClientLogin"]; NSMutableURLRequest* authRequest = [[NSMutableURLRequest alloc] initWithURL:authUrl]; [authRequest setHTTPMethod:@"POST"]; [authRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-type"]; [authRequest setHTTPBody:[content dataUsingEncoding:NSASCIIStringEncoding]]; NSHTTPURLResponse* authResponse; NSError* authError; NSData * authData = [NSURLConnection sendSynchronousRequest:authRequest returningResponse:&authResponse error:&authError]; NSString *authResponseBody = [[NSString alloc] initWithData:authData encoding:NSASCIIStringEncoding]; //loop through response body which is key=value pairs, seperated by \n. The code below is not optimal and certainly error prone. NSArray *lines = [authResponseBody componentsSeparatedByString:@"\n"]; NSMutableDictionary* token = [NSMutableDictionary dictionary]; for (NSString* s in lines) { NSArray* kvpair = [s componentsSeparatedByString:@"="]; if ([kvpair count]>1) [token setObject:[kvpair objectAtIndex:1] forKey:[kvpair objectAtIndex:0]]; } //if google returned an error in the body [google returns Error=Bad Authentication in the body. which is weird, not sure if they use status codes] if ([token objectForKey:@"Error"]) { //handle error };
下一步是让您的应用在Google应用引擎上运行,为您提供ASCID Cookie.我不确定为什么会有这个额外的步骤,这似乎是谷歌的一个问题,也可能是为什么GAE目前不在他们列出的obj-c谷歌数据api库中.我的测试显示我必须请求cookie以便与GAE同步.另外,请注意我不对cookie做任何事情.它似乎只是通过请求和烹饪,未来的请求将自动包含cookie.我不确定这是否是iPhone的东西我的应用程序是一个iPhone应用程序,但我不完全了解这个cookie发生了什么.注意:使用"myapp.appspot.com".
NSURL* cookieUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://myapp.appspot.com/_ah/login?continue=http://myapp.appspot.com/&auth=%@", [token objectForKey:@"Auth"]]]; NSLog([cookieUrl description]); NSHTTPURLResponse* cookieResponse; NSError* cookieError; NSMutableURLRequest *cookieRequest = [[NSMutableURLRequest alloc] initWithURL:cookieUrl]; [cookieRequest setHTTPMethod:@"GET"]; NSData* cookieData = [NSURLConnection sendSynchronousRequest:cookieRequest returningResponse:&cookieResponse error:&cookieError];
最后,我可以将json发布到我的gae app.注意:下面的代码段是异步请求.我们可以通过实现didReceiveResponse,didReceiveData,didFailWIthError来处理响应.
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://myapp.appspot.com/addRun?auth=%@", mytoken]]; NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:@"my http body"; NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (!connectionResponse) { NSLog(@"Failed to submit request"); } else { NSLog(@"Request submitted"); }