6

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.

Community
  • 1
  • 1
sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • hey .. have you got any way to do this..? I am also looking for solution to this. – va05 May 17 '16 at 17:16
  • I am continuing to work on this. I don't think there is a set Google API for it so I am attempting a manual AFNetworking request on an authenticated Google url. Like one of the urls here: https://developers.google.com/+/domains/api/circles/get#request post a comment or answer if you have any breakthroughs – sam_smith May 18 '16 at 01:14
  • Please let me know if you are able to access contacts into your application. I too have the same requirement in my current project – Dee Nov 07 '16 at 21:29
  • I am also looking achieve this but dont have any success :( – Umar Farooq Dec 14 '16 at 06:18
  • https://stackoverflow.com/questions/40163529/integrate-google-contacts-api-into-my-swift-3-app/54710237#54710237 – Naresh Feb 15 '19 at 13:25

1 Answers1

6

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.

sam_smith
  • 6,023
  • 3
  • 43
  • 60
  • 1
    very good detailed question and answer. it worked for me. but it is giving google+ contacts added in my circle. Is there any way we can get all gmail contacts ? – Haris Jun 16 '16 at 10:47
  • @simon_smiley I followed your suggestions to get Google contacts, but, getting errors from google. Can you check this once, https://stackoverflow.com/questions/51614767/not-able-to-fetch-google-contacts-by-calling-api-in-swift – Anilkumar iOS - ReactNative Aug 01 '18 at 06:28
  • @AnilkumariOSdeveloper I found the entire process above incredibly difficult to replicate reliably. I would recommend splitting the two issues (calendar and contacts) to allow you to concentrate on the harder one (contacts). Then I would focus on why you are getting the 403 error. This answer seems to be the same question but opposite (they can't get the calendar to work): https://stackoverflow.com/questions/27130743/google-contact-api-v3-403-forbidden – sam_smith Aug 01 '18 at 06:34
  • @simon_smiley thanks so much for your quick response, but, I am already getting calendar events successfully and the issue with only to contacts fetching even I enabled those API's permission to my project in google developer settings. – Anilkumar iOS - ReactNative Aug 01 '18 at 06:45
  • But, still showing insufficient permission even I have enabled those api's to my app. – Anilkumar iOS - ReactNative Aug 01 '18 at 06:50
  • @simon_smiley I am waiting for your suggestion on this. – Anilkumar iOS - ReactNative Aug 01 '18 at 10:29
  • I would recommend trying to get the contacts working in a separate project if only to allow you see if you made a mistake the first time. Otherwise look through the Google documentation for help. This answer is two years old so I can’t be much help off the top of my head other than recommending following the instructions above carefully. – sam_smith Aug 01 '18 at 11:30