私はWebサーバープログラミングに慣れていないので、これを間違った方法で行っている可能性がありますが、これが私がやろうとしていることです。
ユーザーは、サーバー「https://www.googleapis.com/auth/plus.me」に対してネイティブiOSアプリを使用して認証します。承認を処理するために、 GTMOAuth2クラスを使用しています。有効なトークンを正常に取得したので、この部分は機能します。
手順1で取得した認証トークンを使用して、認証済みユーザーとしてgoogle-app-engineWebサーブレットにJSONデータをPOSTしようとしています。使用しているコードは次のとおりです。
- (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は、ユーザーがログインしていることを示すことはありません。いくつかの異なることを試しましたが、成功しませんでした。私のアプリは「最初にログインする必要があります!」というメッセージが表示されます。エラーメッセージ。何か案は?