how to use GTMOAuth2 to get user gplus friends on ios

63 Views Asked by At

My ios app sign in the gmail on GTMOAth2. And than, can I get the gplus friends email or can I use GTMOAth2 token to get the gplus information?

1

There are 1 best solutions below

1
Kishore Kumar On

1.Create Button

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.Login Authentication

   - (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);

    }

if you like to use token use the above code .......hopes this will help you.