Using Google+ iOS API how to get logged in user's profile details?

前端 未结 3 1363
猫巷女王i
猫巷女王i 2021-01-31 12:17

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, Stackoverf

3条回答
  •  萌比男神i
    2021-01-31 12:51

    First, follow this instructions to setup your project in Google API Console (and to download configuration file GoogleService-Info.plist) and to integrate the latest GoogleSignIn SDK
    into your project: Integration Guide

    A piece of code.

    Here is the code for getting user info with the latest version of GoogleSignIn SDK at this moment.

    #import 
    //...
    
    @interface ViewController() 
    //...
    
    - (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 {
            NSString *userId   = user.userID;
            NSString *fullName = user.profile.name;
            NSString *email    = user.profile.email;
            NSURL *imageURL    = [user.profile imageURLWithDimension:1024];
            NSString *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];
    }
    

    Your AppDelegate.m

    #import 
    //...
    @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 NSString *)sourceApplication annotation:(nonnull id)annotation {
        //...
        return [[GIDSignIn sharedInstance] handleURL:url
                                   sourceApplication:sourceApplication
                                          annotation:annotation];
    }
    
    @end
    

    Also make sure that you've added URL Scheme with your REVERSED_CLIENT_ID which you can find in GoogleService-Info.plist.


    To be able to get list of your connections in Google+ you need to enable Google People API and Google+ API for your project in Google API Console.

    Then just send HTTP request to the following url: https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=YOUR_ACCESS_TOKEN


    To check if user has a Google+ profile just send HTTP request to the following url: https://www.googleapis.com/plus/v1/people/USER_ID?access_token=YOUR_ACCESS_TOKEN and check the value of "isPlusUser" which will be "true" or "false"

    Little bonus
    EXAMPLE PROJECT

提交回复
热议问题