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

how to get UserID from Google plus Oauth2.0 for IOS

我使用了 Google 开发人员文档,但我无法使用 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];

// 您之前在 "Initialize the Google+ client" 步骤中设置了 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:(空)

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

你只需要使用

[GPPSignIn sharedInstance]

像这样:

NSString * gpUserID = [GPPSignIn sharedInstance].userID;