I've managed to successfully log in to google plus from an iPhone app. But how to get logged in user's details? such as profile id, email etc.
I tried this, Stackoverflow answer for a similar question but I was unable to get it working. In that sample what exactly is being passed to accessTocken here,
NSString *str = [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken];
I've implemented that code in - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
error: (NSError *) error
{ }
method. however auth.accessToken
returns a null value.
so I can not use auth.accessToken to append that URL. Is there any other way to get this done?
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error {
NSLog(@"Received Error %@ and auth object==%@", error, auth);
if (error) {
// Do some error handling here.
} else {
[self refreshInterfaceBasedOnSignIn];
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:@"me"];
NSLog(@"email %@ ", [NSString stringWithFormat:@"Email: %@",[GPPSignIn sharedInstance].authentication.userEmail]);
NSLog(@"Received error %@ and auth object %@",error, auth);
// 1. Create a |GTLServicePlus| instance to send a request to Google+.
GTLServicePlus* plusService = [[GTLServicePlus alloc] init] ;
plusService.retryEnabled = YES;
// 2. Set a valid |GTMOAuth2Authentication| object as the authorizer.
[plusService setAuthorizer:[GPPSignIn sharedInstance].authentication];
// 3. Use the "v1" version of the Google+ API.*
plusService.apiVersion = @"v1";
[plusService executeQuery:query
completionHandler:^(GTLServiceTicket *ticket,
GTLPlusPerson *person,
NSError *error) {
if (error) {
//Handle Error
} else {
NSLog(@"Email= %@", [GPPSignIn sharedInstance].authentication.userEmail);
NSLog(@"GoogleID=%@", person.identifier);
NSLog(@"User Name=%@", [person.name.givenName stringByAppendingFormat:@" %@", person.name.familyName]);
NSLog(@"Gender=%@", person.gender);
}
}];
}
}
Hope, this will help you
This is the easiest and the simplest way of fetching the current logged in user's email id first create an instance variable of GPPSignIn class
GPPSignIn *signIn;
then initialize it in the viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
static NSString * const kClientID = @"your client id";
signIn = [GPPSignIn sharedInstance];
signIn.clientID= kClientID;
signIn.scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil];
signIn.shouldFetchGoogleUserID=YES;
signIn.shouldFetchGoogleUserEmail=YES;
signIn.delegate=self;
}
next implement the GPPSignInDelegate
in your view controller
here you can get the logged in user's email id
- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
error:(NSError *)error
{
NSLog(@"Received Access Token:%@",auth);
NSLog(@"user google user id %@",signIn.userEmail); //logged in user's email id
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With