iOS:Google认证码 [英] iOS: Google Authentication Code
问题描述
我正在验证用户使用他与之关联的Google帐户。问题是,每当用户通过我的应用程序登录时,即使我点击了先前测试中的允许访问,Google的身份验证视图中仍会显示允许访问。这是正常的还是我在做我的代码错误?请帮助我们。
我在下面的代码中使用了以下代码:
- (IBAction)signIn:(id)sender {
if(!isSignedIn){
[self signOutFromAll];
NSString * keychainItemName = nil;
//保存钥匙串
keychainItemName = kKeychainItemName;
NSString * scope = @https://www.googleapis.com/auth/plus.me;
NSString * clientID = kClientID;
NSString * clientSecret = kClientSecret;
SEL finishedSel = @selector(viewController:finishedWithAuth:error :);
GTMOAuth2ViewControllerTouch * viewController;
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:keychainItemName
delegate:self
finishedSelector:finishedSel];
[[self navigationController] pushViewController:viewController animated:YES];
} else {
[self displayAlertWithMessage:@Currently currently。];
}}
- (IBAction)signOut:(id)sender {
[self signOutFromAll];
[self displayAlertWithMessage:@签出。]; }
这是代表:
<$ (GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
错误:(NSError *)错误{
if(错误!=无){
//认证失败...
NSLog(@认证错误:%@,错误);
NSData * responseData = [[error userInfo] objectForKey:@data];
if([responseData length]> 0)
NSLog(@%@,[[[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] autorelease]);
self.auth = nil;
} else {
//认证成功...
isSignedIn = YES;
self.auth = auth;
$ / code>
和awakeFromNib:
- (void)awakeFromNib {
//填写Client ID和Client Secret文本字段
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults] ;
//首先,我们将尝试从钥匙串
中获取已保存的Google身份验证(如果有的话)//普通应用程序将在其客户端ID和客户端密钥中进行硬编码
//但是示例应用程序允许用户在文本字段中输入它们,并将它们保存在首选项中
NSString * clientID = [defaults stringForKey:kGoogleClientIDKey];
NSString * clientSecret = [defaults stringForKey:kGoogleClientSecretKey];
GTMOAuth2Authentication * auth;
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
if(auth.canAuthorize){
//有保存的google身份验证
// self.serviceSegments.selectedSegmentIndex = 0;
}
//保存认证对象,认证对象持有认证令牌
self.auth = auth;
[self setAuth:auth];
isSignedIn = self.auth.canAuthorize;
}
顺便说一下,我对这些代码的引用是在这个链接上: http://code.google.com/p/gtm-oauth2/wiki/Introduction #Using_the_OAuth_2_Controllers
当您将oauth对象保存到钥匙串中时,请使用此方法。
$ b
[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth];
和
api只是使用这个检查和检索oauth对象
GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME
clientID:GOOGLE_CLIENT_KEY
clientSecret:GOOGLE_CLIENT_SECRET];
并确保它的oauth对象是真实的,使用它
if(![GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth])
I am working with authenticating user to use the google account he is associated with. The problem is that everytime the user logs in through my app, the "Allow Access" always appears on the Google's authentication view even I had clicked the Allow Access already from previous test. Is this normal or am I doing my codes wrong? Please help me guys.
I used the following codes for loggin in an out:
- (IBAction)signIn:(id)sender {
if(!isSignedIn){
[self signOutFromAll];
NSString *keychainItemName = nil;
// save keychain
keychainItemName = kKeychainItemName;
NSString *scope = @"https://www.googleapis.com/auth/plus.me";
NSString *clientID = kClientID;
NSString *clientSecret = kClientSecret;
SEL finishedSel = @selector(viewController:finishedWithAuth:error:);
GTMOAuth2ViewControllerTouch *viewController;
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope
clientID:clientID
clientSecret:clientSecret
keychainItemName:keychainItemName
delegate:self
finishedSelector:finishedSel];
[[self navigationController]pushViewController:viewController animated:YES];
} else {
[self displayAlertWithMessage:@"Currently Signed in."];
} }
- (IBAction)signOut:(id)sender {
[self signOutFromAll];
[self displayAlertWithMessage:@"Signed out."]; }
This is for the delegate:
- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController
finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error{
if(error != nil){
// Authentication failed...
NSLog(@"Authentication error: %@", error);
NSData *responseData = [[error userInfo] objectForKey:@"data"];
if([responseData length] > 0)
NSLog(@"%@", [[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]autorelease]);
self.auth = nil;
} else {
// Authentication succeeded...
isSignedIn = YES;
self.auth = auth;
}
}
And awakeFromNib:
- (void)awakeFromNib{
// Fill in the Client ID and Client Secret text fields
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
// First, we'll try to get the saved Google authentication, if any, from the keychain
// Normal applications will hardcode in their client ID and client secret,
// But the sample app allows the user to enter them in a text field, and saves them in the preferences
NSString *clientID = [defaults stringForKey:kGoogleClientIDKey];
NSString *clientSecret = [defaults stringForKey:kGoogleClientSecretKey];
GTMOAuth2Authentication *auth;
auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
clientID:clientID
clientSecret:clientSecret];
if (auth.canAuthorize) {
// There is saved google authentication
// self.serviceSegments.selectedSegmentIndex = 0;
}
// Save the authentication object, which holds the auth tokens
self.auth = auth;
[self setAuth:auth];
isSignedIn = self.auth.canAuthorize;
}
By the way my reference for these codes is on this link: http://code.google.com/p/gtm-oauth2/wiki/Introduction#Using_the_OAuth_2_Controllers
Use this method when you get the oauth object to save into keychain
[GTMOAuth2ViewControllerTouch saveParamsToKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth];
and
before making a call to api just check and retrieve the oauth object using this
GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch
authForGoogleFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME
clientID:GOOGLE_CLIENT_KEY
clientSecret:GOOGLE_CLIENT_SECRET];
and make sure it's oauth object is authentic with using this
if(![GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:YOUR_KEYCHAIN_ITEM_NAME authentication:auth])
这篇关于iOS:Google认证码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!