如何从Google获取UserID以及IOS获取Oauth2.0

时间:2015-08-18 08:24:41

标签: ios objective-c oauth-2.0 google-plus

我使用了Google Developers Documentation但我无法使用UserID

这是我的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.0f/255.0f green:102.0f/255.0f blue:204.0f/255.0f alpha:100.0];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    signIn = [GPPSignIn sharedInstance];

//您之前在“初始化Google+客户端”步骤

中设置了kClientId
    signIn.clientID = kClientId;
    [signIn setScopes:[NSArray arrayWithObject: @"https://www.googleapis.com/auth/plus.me"]];

//取消注释以获取用户的电子邮件

     signIn.shouldFetchGoogleUserEmail = YES;

//取消注释以获取用户的ID

     signIn.shouldFetchGoogleUserID = YES;
    signIn.delegate = self;

    Gplusbtn = [[GPPSignInButton alloc] init];
    Gplusbtn.frame = CGRectMake([simod CG:10], [simod CG:125], [simod CG:300], [simod CG:30]);
    Gplusbtn.style=kGPPSignInButtonStyleWide;
    Gplusbtn.enabled = YES;
    [self.view addSubview:Gplusbtn];
}


- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
                   error:(NSError *)error {
    if (error) {
   _signInAuthStatus.text =
   [NSString stringWithFormat:@"Status: Authentication error: %@", error];
        return;
    }
NSLog(@"ID:%@", signIn.userID);
}

NSLog显示ID :( null)

那么代码有问题,如何解决?

1 个答案:

答案 0 :(得分:1)

你只需要使用

  

[GPPSignIn sharedInstance]

像这样:

NSString * gpUserID = [GPPSignIn sharedInstance].userID;