Google Oauth2プラグインを利用してGoogle認証画面を呼び出す
googleコード:
-(void)authThroughGoogle
{
NSString *clientId = GOOGLE_CLIENT_ID;
NSString *clientSecret = GOOGLE_CLIENT_SECRET;
NSString *scope = GOOGLE_AUTH_SCOPE;
GTMOAuth2ViewControllerTouch *authViewController = [[[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope clientID:clientId clientSecret:clientSecret keychainItemName:kKeyChainGoogleAuth delegate:self finishedSelector:@selector(viewController:finishedWithAuth:error:)]autorelease];
NSString *html = @"<html><body bgcolor=white><div align=center>Loading…</div></body></html>";
authViewController.initialHTMLString = html;
[self.navigationController pushViewController:authViewController animated:YES];
}
サーバ側からAuth Tokenを取得
-(void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error{
if (error!=nil) {
NSLog(@"認証失敗!");
[self showAlertView:[error localizedDescription]];
}else{
NSLog(@"認証成功!: %@", [auth accessToken]);
NSString *token = [AuthHelper getAuthTokenThroughGoogle:[auth accessToken]];
if(token != nil){
[[NSUserDefaults standardUserDefaults] setObject:token forKey:APP_NAME];
[self goToMainPage];
}else{
[self showAlertView:@"G authorize tokenを取得した"];
}
}
}