Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Google contacts using API on iOS

I have added Google login to my app which uses Firebase to login with social media. I am now trying to access the friends of the Google user. I want to return a list of friends/contacts so I can search to see if they are currently using the app.

I have found very little information on this so far.

There are a number of stackoverflow questions which are unanswered asking a similar thing.

The Google API has been updated from a Google+ login to a Google Signin API meaning that whereas previously we could find sharing code now they have much less information available.

This means that some answers seem likely to be depreciated by now.

There are also a number of answers that say it is not possible to do this.

My issue is that there don't seem to be any answers or questions within the last year and the new GoogleSignIn was only released just over a year ago.

I want to know whether it is possible to retrieve a list of the user's friends or contacts in any way, shape or form when logged in with a GIDGoogleUser object. Ideally I would return some information on them but even if it was just their name this would be good enough.

Facebook API allows a user to return the details of users who have signed in to the app but not on all users. Is there something similar I am missing for Google or is there no way currently, using the official API, to do this.

Any help on whether this is possible or not would be very helpful to allow me to plan the next moves in my app.

like image 985
sam_smith Avatar asked May 16 '16 06:05

sam_smith


People also ask

How do I access my Google Contacts API?

To access personal contacts: https://www.googleapis.com/auth/contacts. To access directory information: https://www.googleapis.com/auth/directory.readonly.

Does Google Contacts have an app for iPhone?

Every number you add to your phone becomes a contact in Google Contacts, which means any phone numbers you save on your phone will also show up on your computer. There's no Google Contacts app for iPhone or iPad, but you can sync your Google Contacts with Apple's Contacts app if you want.


1 Answers

This question was made more difficult by the number of Google API versions in the last few years and also how rarely users actually login with their Google details. This means there is little documentation due to the lack of demand. I finally managed to get it working with the help of this great swift tutorial by App Code. This tutorial is rare as it goes through the entire signin process step by step as well as covering contact look up which I what I was after.

I would recommend going through this tutorial start to finish but I will also put some of the most important information here.

The first thing to do is ensure you have added your app correctly to the Google Console here. Make sure to add the Google+ API and get your client ID.

Second you need to set up your Google sign in. This is pretty easy and there are some great resources for setting this up. I would recommend this Google youtube video but there are a lot of other resources online. I won't go into detail as this is not the point of the question.

Next you need to set your sign in code up.

Make sure you have added both the delegates to your viewController

<GIDSignInDelegate, GIDSignInUIDelegate

and also configured it in the viewDidLoad:

[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].uiDelegate = self;
[GIDSignIn sharedInstance].clientID = @"123456789012-thisismadeup12345678901234567890.apps.googleusercontent.com";

[[GIDSignIn sharedInstance] setScopes:@[@"https://www.googleapis.com/auth/plus.login", @"https://www.googleapis.com/auth/plus.me"]];

None of the answers I found elsewhere had me setting my clientID (from google console) but the important part is adding the scopes code, without this you haven't got the correct permissions to get contacts.

At this stage you can check if everything is set up correctly. Load up your app and click the Google login button and it should load up the webview and ask you to confirm your permissions. It should look like this:

enter image description here

The important permissions are the top two. If you only have the bottom two you will only be able to access your own profile and not those of your friends.

Once you have got this far you only need to perform the correct API call to return your friends. This is easier said than done with the pure number of different ones on different Google websites. I completed mine with the following:

NSString * urlString = [NSString stringWithFormat:@"https://www.googleapis.com/plus/v1/people/me/people/visible?access_token=%@", [GIDSignIn sharedInstance].currentUser.authentication.accessToken];

AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];

AFJSONResponseSerializer *responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer = responseSerializer;

[manager GET:path parameters:params success:^(AFHTTPRequestOperation * operation, id responseObject) {

    if (![responseObject isEqual: [NSNull null]]) {

        if(responseObject[@"error"]) {
            NSError * error = [NSError errorWithDomain:@"" code:0 userInfo:@{NSLocalizedDescriptionKey: responseObject[@"error"]}];
        }
        else {
            // We now have our response object
        }
    }
    else {
        // response is nil
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // Failure
}];

You will need to add AFNetworking to your project and this is a custom function for my project so might not be copy past-able.

This though should return a dictionary of all your contacts which you can then use.

like image 91
sam_smith Avatar answered Oct 04 '22 22:10

sam_smith