-1

我的 ios 应用程序在 GTMOAth2 上登录了 gmail。然后,我可以获取 gplus 朋友的电子邮件,还是可以使用 GTMOAth2 令牌来获取 gplus 信息?

4

1 回答 1

1

1.创建按钮

signIn = [GPPSignIn sharedInstance];
    signIn.delegate = self;
     signIn.clientID = kClientId;
    signIn.shouldFetchGooglePlusUser = YES;
    signIn.shouldFetchGoogleUserEmail = YES;
        signIn.homeServerClientID = kClientId;
    // Uncomment one of these two statements for the scope you chose in the previous step
    //    signIn.scopes = @[ kGTLAuthScopePlusLogin ];  // "https://www.googleapis.com/auth/plus.login" scope
    signIn.scopes = @[ @"profile" ];           
    signIn.shouldFetchGoogleUserEmail=YES;
    [signIn trySilentAuthentication];

2.登录认证

   - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                           error: (NSError *) error {
            NSLog(@"Received error %@ and auth object %@",error, auth);
            if(error)
            {

            }
            else
            {


                    NSString *serverCode = [GPPSignIn sharedInstance].homeServerAuthorizationCode;
                    NSLog(@"this is server code%@",serverCode);
                    NSLog(@"user email%@", signIn.authentication.userEmail);
                    NSLog(@"user id%@",signIn.authentication.userID);
                    NSLog(@"auth token=%@",auth.accessToken);
                    NSString *str =  [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",auth.accessToken];
                    NSString* escapedUrl = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
                    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",escapedUrl]];
                    NSString *jsonData = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
                    NSData *data = [jsonData dataUsingEncoding:NSUTF8StringEncoding];
                    id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];




                    name = json[@"given_name" ];
                    email=json[@"email"];
                    imgurl=json[@"picture"];
                    NSLog(@"this is email=%@name==%@picture url%@",email,name,imgurl);

    }

如果您喜欢使用令牌,请使用上面的代码.......希望这会对您有所帮助。

于 2015-11-22T14:37:00.513 回答