来自IOS的Google登录错误

时间:2014-09-17 12:12:25

标签: ios authentication google-api google-oauth

尝试验证时出现以下错误: {错误:" unauthorized_client"}

我为一小部分用户收到此错误。我在同一设备上用两个不同的用户测试了它,我能够用一个而不是另一个用户登录。所以它与帐户而不是设备有关。

发送请求后,登录过程会在"请等待..."屏幕。

发生一次后,我需要删除该应用并重新安装才能再次进入登录屏幕。

有什么想法吗?

#import "Define.h"
#import "TIGoogleOAuthViewController.h"
#import "TIWebClient.h"
#import "TIAppDelegate.h"

@interface TIGoogleOAuthViewController ()
{
    User * _googleUserInfo;
}

@end

@implementation TIGoogleOAuthViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        _googleUserInfo = nil;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


    self.googleOAuth = [[GoogleOAuth alloc] initWithFrame:self.view.frame];
    [self.googleOAuth setGOAuthDelegate:self];
    [self.googleOAuth authorizeUserWithClienID:kGoogleClientID
                           andClientSecret:kGoogleClientSecKEY
                             andParentView:self.view
                                 andScopes:[NSArray arrayWithObjects:@"https://www.googleapis.com/auth/userinfo.profile",
                                            @"https://www.googleapis.com/auth/userinfo.email", nil]
     ];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Google OAuth delegate

#pragma mark GoogleOAuth Delegate
-(void)authorizationWasSuccessful
{
    [self.googleOAuth callAPI:@"https://www.googleapis.com/oauth2/v1/userinfo"
           withHttpMethod:httpMethod_GET
       postParameterNames:nil postParameterValues:nil];
}

-(void)accessTokenWasRevoked
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                    message:@"Your access was revoked!"
                                                   delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [alert show];
}

-(void)errorOccuredWithShortDescription:(NSString *)errorShortDescription andErrorDetails:(NSString *)errorDetails
{
    NSLog(@"%@", errorShortDescription);
    NSLog(@"%@", errorDetails);
}

-(void)errorInResponseWithBody:(NSString *)errorMessage
{
    NSLog(@"%@", errorMessage);
}

-(void)responseFromServiceWasReceived:(NSString *)responseJSONAsString andResponseJSONAsData:(NSData *)responseJSONAsData
                    access_token_file:(NSString*)access_token_file refresh_token_file:(NSString*)refresh_token_file
{

    if ([responseJSONAsString rangeOfString:@"family_name"].location != NSNotFound) {
        NSError *error = nil;
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseJSONAsData
                                                                   options:NSJSONReadingMutableContainers
                                                                     error:&error];
        if (error)
        {
            NSLog(@"An error occured while converting JSON data to dictionary.");
            return;
        } else
        {
            NSLog(@"google responde = %@", dic);

            _googleUserInfo = [[User alloc] init];

            _googleUserInfo.email = [dic objectForKey:@"email"];
            _googleUserInfo.firstname = [dic objectForKey:@"given_name"];
            _googleUserInfo.lastname = [dic objectForKey:@"family_name"];
            _googleUserInfo.image_profile = [dic objectForKey:@"picture"];

            NSMutableDictionary * tokenDic = [[NSMutableDictionary alloc] initWithContentsOfFile:access_token_file];
            _googleUserInfo.google_access_token = [tokenDic objectForKey:@"access_token"];

            //save to permanent storage
            NSUserDefaults *pref = [NSUserDefaults standardUserDefaults];
            [pref setValue:access_token_file forKey:@"access_token_file"];
            [pref setValue:refresh_token_file forKey:@"refresh_token_file"];
            [pref synchronize];

            //notify to web server
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(void)
                           {
                               [[TIWebClient getInstance] googleSignIn:_googleUserInfo andDelegate:self];

                           });
        }
    }
}

#pragma mark - Login delegate
-(void) didLoginFailed
{
    NSLog(@"Google Login failed");
}

-(void) didLoginSuccess:(User *)user type:(NSInteger)type
{
    TIAppDelegate * app = (TIAppDelegate *)[[UIApplication sharedApplication] delegate];
    [DataManager dataManager].mine = user;
    [DataManager putPreference:YES :[DataManager dataManager].mine];

    [app showMainView:nil];
}

@end

0 个答案:

没有答案