À l'aide de l'API Google+ iOS, comment se connecter aux détails du profil de l'user?

J'ai réussi à me connecter à google plus à partir d'une application iPhone. Mais comment se connecter aux détails de l'user? tel que l'identification de profil, l'email etc.

J'ai essayé ceci, réponse de Stackoverflow pour une question semblable mais j'étais incapable de le faire fonctionner. Dans cet exemple, qu'est-ce qui est exactement passé à accessTocken ici,

NSSsortingng *str = [NSSsortingng ssortingngWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken]; 

J'ai implémenté ce code dans - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { } méthode. cependant, auth.accessToken renvoie une valeur nulle.

donc je ne peux pas utiliser auth.accessToken pour append cette URL. Y a-t-il un autre moyen d'y parvenir?

 - (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 %@ ", [NSSsortingng ssortingngWithFormat:@"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 ssortingngByAppendingFormat:@" %@", person.name.familyName]); NSLog(@"Gender=%@", person.gender); } }]; } } 

J'espère que ceci vous aidera

C'est le moyen le plus simple et le plus simple d'aller chercher l'identifiant actuel de l'user dans l'email. Créez d'abord une variable d'instance de la class GPPSignIn.

 GPPSignIn *signIn; 

puis l'initialiser dans viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; static NSSsortingng * 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; } 

implémenter ensuite le GPPSignInDelegate dans votre controller de vue ici, vous pouvez get l'identifiant de l'user de l'user connecté

  - (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 } 

Tout d'abord, suivez ces instructions pour configurer votre projet dans Google API Console (et pour download le file de configuration GoogleService-Info.plist) et pour intégrer le dernier SDK GoogleSignIn.
dans votre projet: Guide d'intégration

Un morceau de code.

Voici le code pour get des informations d'user avec la dernière version de GoogleSignIn SDK en ce moment.

 #import <GoogleSignIn/GoogleSignIn.h> //... @interface ViewController() <GIDSignInDelegate, GIDSignInUIDelegate> //... - (IBAction)googleSignInTapped:(id)sender { [GIDSignIn sharedInstance].clientID = kClientId;// Replace with the value of CLIENT_ID key in GoogleService-Info.plist [GIDSignIn sharedInstance].delegate = self; [GIDSignIn sharedInstance].shouldFetchBasicProfile = YES; //Setting the flag will add "email" and "profile" to scopes. NSArray *additionalScopes = @[@"https://www.googleapis.com/auth/contacts.readonly", @"https://www.googleapis.com/auth/plus.login", @"https://www.googleapis.com/auth/plus.me"]; [GIDSignIn sharedInstance].scopes = [[GIDSignIn sharedInstance].scopes arrayByAddingObjectsFromArray:additionalScopes]; [[GIDSignIn sharedInstance] signIn]; } #pragma mark - GoogleSignIn delegates - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { if (error) { //TODO: handle error } else { NSSsortingng *userId = user.userID; NSSsortingng *fullName = user.profile.name; NSSsortingng *email = user.profile.email; NSURL *imageURL = [user.profile imageURLWithDimension:1024]; NSSsortingng *accessToken = user.authentication.accessToken; //Use this access token in Google+ API calls. //TODO: do your stuff } } - (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error { // Perform any operations when the user disconnects from app here. } - (void)signIn:(GIDSignIn *)signIn presentViewController:(UIViewController *)viewController { [self presentViewController:viewController animated:YES completion:nil]; } // Dismiss the "Sign in with Google" view - (void)signIn:(GIDSignIn *)signIn dismissViewController:(UIViewController *)viewController { [self dismissViewControllerAnimated:YES completion:nil]; } 

Votre AppDelegate.m

 #import <GoogleSignIn/GoogleSignIn.h> //... @implementation AppDelegate //... - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //... return YES; } //For iOS 9.0 and later - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options { //... return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey] annotation:options[UIApplicationOpenURLOptionsAnnotationKey]]; } //For your app to run on iOS 8 and older, - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSSsortingng *)sourceApplication annotation:(nonnull id)annotation { //... return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation]; } @end 

Assurez-vous également que vous avez ajouté le schéma d'URL avec votre REVERSED_CLIENT_ID que vous pouvez find dans GoogleService-Info.plist.


Pour pouvoir get la list de vos connections dans Google+, vous devez activer l' Google People API et l' Google+ API pour votre projet dans Google API Console .

Ensuite, envoyez simplement une requête HTTP à l' adresse suivante: https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=YOUR_ACCESS_TOKEN


Pour vérifier si l'user a un profil Google+ envoyez simplement une requête HTTP à l' adresse suivante: https://www.googleapis.com/plus/v1/people/USER_ID?access_token=YOUR_ACCESS_TOKEN et vérifiez la valeur de "isPlusUser" qui sera "true" ou "false"

entrez la description de l'image ici

Petit bonus
EXEMPLE DE PROJET