我是网络服务器编程的新手,所以也许我做错了,但这就是我想要做的:
用户使用本机 iOS 应用对服务器“https://www.googleapis.com/auth/plus.me”进行身份验证。我正在使用 GTMOAuth2 类来处理授权。我已成功检索到有效令牌,因此这部分有效。
我现在正在尝试使用我在步骤 1 中获得的身份验证令牌将 JSON 数据作为经过身份验证的用户发布到 google-app-engine web servlet。这是我正在使用的代码:
- (void)postDictionary:(NSMutableDictionary *)dict toURL:(NSMutableString *)urlString withAuthToken:(GTMOAuth2Authentication *)authToken delegate:(id)connectDelegate { if( ![NSJSONSerialization isValidJSONObject:dict] ) { NSLog(@"JSON Data is Invalid!"); return; } NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil]; NSMutableString *serverURL = [[NSMutableString alloc] initWithString:urlString]; //DEBUG NSLog(@"sent URL: %@", serverURL); NSURL *url = [[NSURL alloc] initWithString:serverURL]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; // Execute URL Asynchronously // Make sure we "Post" the data to the server [request setHTTPMethod:@"POST"]; [request setHTTPBody:jsonData]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; [request setValue:@"json" forHTTPHeaderField:@"Data-Type"]; [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // We need to send the authentication information to the server. [request setValue:CLIENT_ID forHTTPHeaderField:@"client_id"]; [request setValue:CLIENT_SECRET forHTTPHeaderField:@"client_secret"]; [request setValue:authToken.accessToken forHTTPHeaderField:@"Authorization: GoogleLogin auth"]; // Make sure we can authenticate on HTTP instead of just HTTPS authToken.shouldAuthorizeAllRequests=YES; // USED FOR TESTING ON LOCALHOST ONLY!!! // Let's Try the Fetcher method... GTMHTTPFetcher* myFetcher = [GTMHTTPFetcher fetcherWithRequest:request]; [myFetcher setAuthorizer:authToken]; [myFetcher beginFetchWithDelegate:self didFinishSelector:@selector(myFetcher:finishedWithData:error:)]; return; }
目前我试图发布到的服务器正在“http://localhost:8888/mywebapp”上运行。
在 Web 服务器上,我正在尝试使用以下 java 代码对用户进行身份验证:
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { // Perform Authentication... UserService userService = UserServiceFactory.getUserService(); User user = userService.getCurrentUser(); if( user != null ) { System.out.println("User: "+user.getNickname()); } else { sendError( "You must login first!", resp ); return; } ...
UserService 从不表示用户已登录。我尝试了几种不同的方法,但都没有成功。我的应用程序刚刚收到“您必须先登录!” 错误信息。有任何想法吗?