2014-02-14 81 views
1

我在摹郵件驗證使用GTMOAuth2庫登錄過程中會神,但重定向時收到錯誤的oauth2錯誤

#define GoogleAuthURL @"https://accounts.google.com/o/oauth2/auth" 
    #define GoogleTokenURL @"https://accounts.google.com/o/oauth2/token" 

    NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL]; 

     NSString * redirectURI = @""; 

     GTMOAuth2Authentication * auth; 

     auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"google" 
                   tokenURL:tokenURL 
                   redirectURI:redirectURI 
                   clientID:GoogleClientID 
                  clientSecret:GoogleClientSecret]; 

     auth.scope = @"https://www.googleapis.com/auth/userinfo.profile"; 

     GTMOAuth2ViewControllerTouch * viewcontroller = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth 
                            authorizationURL:[NSURL URLWithString:GoogleAuthURL] 
                            keychainItemName:@"OAuth Sample: Google Contacts" delegate:self 
                            finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 

     [self.navigationController pushViewController:viewcontroller animated:YES]; 




Error Domain=com.google.GTMOAuth2 Code=-1001 "The operation couldn’t be completed. (com.google.GTMOAuth2 error -1001.)" 

請幫我

回答

0

試試下面的代碼工作....它可能將幫助你我的朋友......先在ViewDidLoad中編寫下面的代碼。

- (void)viewDidLoad 
{ 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 

NSString *clientID = [defaults stringForKey:kGoogleClientIDKey]; 
NSString *clientSecret = [defaults stringForKey:kGoogleClientSecretKey]; 

GTMOAuth2Authentication *auth = nil; 

if (clientID && clientSecret) { 
    auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName 
                   clientID:clientID 
                  clientSecret:clientSecret]; 
} 




// Save the authentication object, which holds the auth tokens and 
// the scope string used to obtain the token. For Google services, 
// the auth object also holds the user's email address. 
self.auth = auth; 

// Update the client ID value text fields to match the radio button selection 
[self loadClientIDValues]; 
} 

- (void)loadClientIDValues { 
// Load the client ID and secret from the prefs into the text fields 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
mClientIDField.text = [defaults stringForKey:kGoogleClientIDKey]; 
mClientSecretField.text = [defaults stringForKey:kGoogleClientSecretKey]; 
} 

然後在下面的代碼寫的Gmail登入動作......

-(IBAction)gmailLogin 
{ 
[self saveClientIDValues];//this is the method for saving the credentials of gmail account of particular user. 

if (![self isSignedIn]) { 
    // Sign in 
    [self signInToGoogle]; 

} else { 
    // Sign out 
    [self signOut]; 
} 

} 

- (void)saveClientIDValues { 
// Save the client ID and secret from the text fields into the prefs 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
NSString *clientID = mClientIDField.text; 
NSString *clientSecret = mClientSecretField.text; 

[defaults setObject:clientID forKey:kGoogleClientIDKey]; 
[defaults setObject:clientSecret forKey:kGoogleClientSecretKey]; 
} 

要檢查用戶是否logined與否!!!!

- (BOOL)isSignedIn { 
BOOL isSignedIn = self.auth.canAuthorize; 
return isSignedIn; 
} 

那麼如果用戶沒有登錄,所以做出登錄...

- (void)signInToGoogle { 
[self signOut]; 
NSString *keychainItemName = nil; 
keychainItemName = kKeychainItemName; 

// For Google APIs, the scope strings are available 
// in the service constant header files. 
NSString *scope = @"https://www.googleapis.com/auth/plus.me"; 

// Typically, applications will hardcode the client ID and client secret 
// strings into the source code; they should not be user-editable or visible. 
// 
// But for this sample code, they are editable. 
NSString *clientID = @"Your Id"; 
NSString *clientSecret = @"Your Secret"; 

if ([clientID length] == 0 || [clientSecret length] == 0) { 
// NSString *msg = @"The sample code requires a valid client ID and client secret to sign in."; 
    return; 
} 

// Note: 
// GTMOAuth2ViewControllerTouch is not designed to be reused. Make a new 
// one each time you are going to show it. 

// Display the autentication view. 
SEL finishedSel = @selector(viewController:finishedWithAuth:error:); 

GTMOAuth2ViewControllerTouch *viewController; 
viewController = [GTMOAuth2ViewControllerTouch controllerWithScope:scope 
                  clientID:clientID 
                 clientSecret:clientSecret 
                keychainItemName:keychainItemName 
                  delegate:self 
                finishedSelector:finishedSel]; 

NSDictionary *params = [NSDictionary dictionaryWithObject:@"en" 
                forKey:@"hl"]; 
viewController.signIn.additionalAuthorizationParameters = params; 

// By default, the controller will fetch the user's email, but not the rest of 
// the user's profile. The full profile can be requested from Google's server 
// by setting this property before sign-in: 
// 
viewController.signIn.shouldFetchGoogleUserProfile = YES; 
// 
// The profile will be available after sign-in as 
// 
// NSDictionary *profile = viewController.signIn.userProfile; 
NSDictionary *profile = viewController.signIn.userProfile; 
NSLog(@"%@",profile); 
// Optional: display some html briefly before the sign-in page loads 
NSString *html = @"<html><body bgcolor=silver><div align=center>Loading sign-in page...</div></body></html>"; 
viewController.initialHTMLString = html; 

[[self navigationController] pushViewController:viewController animated:YES]; 

// The view controller will be popped before signing in has completed, as 
// there are some additional fetches done by the sign-in controller. 
// The kGTMOAuth2UserSignedIn notification will be posted to indicate 
// that the view has been popped and those additional fetches have begun. 
// It may be useful to display a temporary UI when kGTMOAuth2UserSignedIn is 
// posted, just until the finished selector is invoked. 
} 

如果每一件事情是正確的,那麼你會得到導致本以下方法...

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController 
    finishedWithAuth:(GTMOAuth2Authentication *)auth 
      error:(NSError *)error { 
    if (error != nil) { 
    // Authentication failed (perhaps the user denied access, or closed the 
    // window before granting access) 
    NSLog(@"Authentication error: %@", error); 
    NSData *responseData = [[error userInfo] objectForKey:@"data"]; // kGTMHTTPFetcherStatusDataKey 
    if ([responseData length] > 0) { 
     // show the body of the server's authentication failure response 
     NSString *str = [[[NSString alloc] initWithData:responseData 
               encoding:NSUTF8StringEncoding] autorelease]; 
     NSLog(@"%@", str); 
    } 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Eye News" message:@"Gmail Authentication Fail. Please try again" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

    self.auth = nil; 
} 
else 
{ 
    viewController.signIn.shouldFetchGoogleUserProfile = YES; 
    NSDictionary *profile = viewController.signIn.userProfile; 
    NSLog(@"%@",profile); 
} 
} 

讓我知道它工作與否!

快樂編碼!

+0

controllerWithScope:這種方法不GTMOAuth2ViewControllerTouch – user3101600

+0

你有一個名爲GTMOAuth2ViewControllerTouch.h和GTMOAuth2ViewControllerTouch.m這些類文件類???? – NiravPatel

1
//import GTMOAuth2Authentication , GTMOAuth2ViewControllerTouch 

#define GoogleClientID @"paster your client id" 
#define GoogleClientSecret @"paste your client secret" 
#define GoogleAuthURL @"https://accounts.google.com/o/oauth2/auth" 
#define GoogleTokenURL @"https://accounts.google.com/o/oauth2/token" 

-(void) SignInGoogleButtonClicked 
{ 

NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL]; 

    NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob"; 

    GTMOAuth2Authentication * auth; 

    auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"google" 
                  tokenURL:tokenURL 
                  redirectURI:redirectURI 
                  clientID:GoogleClientID 
                 clientSecret:GoogleClientSecret]; 

    auth.scope = @"https://www.googleapis.com/auth/plus.me"; 

    GTMOAuth2ViewControllerTouch * viewcontroller = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth 
                           authorizationURL:[NSURL URLWithString:GoogleAuthURL] 
                           keychainItemName:@"GoogleKeychainName" delegate:self 
                           finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 

    [self.navigationController pushViewController:viewcontroller animated:YES]; 

} 



//this method is called when authentication finished 

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error 
{ 

    if (error != nil) { 

     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error Authorizing with Google" 
                 message:[error localizedDescription] 
                 delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
     [alert show]; 
    } 
    else 
    { 

     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert !" 
                 message:@"success" 
                 delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
     [alert show]; 

    } 
} 

試試這個

+0

兄弟我我導入那些類相同的代碼使用仍然發生錯誤 – user3101600

+0

http://stackoverflow.com/questions/16828135/example-for-gmail-integration-in-ios看這一步 – Sport

相關問題