1

I need to fetch google contact using jquery. and i have success fully implement. but what the issue is i unble to fetch the name of that contact. google is just providing me email address of that user. no other information is provided by user. so am i miss something.

Here i attach full code with response.

Here is full code

<script type="text/javascript" src="https://apis.google.com/js/client.js"></script>
<script type="text/javascript">
    var clientId = "google_clientId";
    var apiKey = "google_api_key";
    var scopes = 'https://www.google.com/m8/feeds/';
    $(document).on("click", ".googleContactsButton", function (e) {
        gapi.client.setApiKey(apiKey);
        window.setTimeout(authorize);
    });
    function authorize() {
        gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
    }

    function handleAuthorization(authorizationResult) {
        if (authorizationResult && !authorizationResult.error) {

            $.get("https://www.google.com/m8/feeds/contacts/default/full?alt=json&access_token=" + authorizationResult.access_token + "&alt=json",
                    function (response) {
                        //process the response here
                        console.log(JSON.stringify(response));

                    });
        }
    }
</script>

Here is Response of the API

[
  {
    "id": {
      "$t": "http://www.google.com/m8/feeds/contacts/ishan%40inheritx.com/base/87427988f9359bf"
    },
    "updated": {
      "$t": "2016-07-21T08:09:55.053Z"
    },
    "category": [
      {
        "scheme": "http://schemas.google.com/g/2005#kind",
        "term": "http://schemas.google.com/contact/2008#contact"
      }
    ],
    "title": {
      "type": "text",
      "$t": ""
    },
    "link": [
      {
        "rel": "http://schemas.google.com/contacts/2008/rel#edit-photo",
        "type": "image/*",
        "href": "https://www.google.com/m8/feeds/photos/media/ishan%40inheritx.com/87427988f9359bf/1B2M2Y8AsgTpgAmY7PhCfg"
      },
      {
        "rel": "self",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/ishan%40inheritx.com/full/87427988f9359bf"
      },
      {
        "rel": "edit",
        "type": "application/atom+xml",
        "href": "https://www.google.com/m8/feeds/contacts/ishan%40inheritx.com/full/87427988f9359bf/1469088595053001"
      }
    ],
    "gd$email": [
      {
        "rel": "http://schemas.google.com/g/2005#other",
        "address": "prakash@inheritx.com"
      }
    ]
  }
]

In response we didn't find any field like first-name and last-name. any help would be appreciate.

But if i added Name manually then it shows me in title key. but if that contact name is sync from google+ then it shows me blank.

Thanks

Ishan Shah
  • 1,665
  • 2
  • 20
  • 42

2 Answers2

1

Add a &v=3 to the end of the URL. This will populate the name field. However, since the title field is empty in the response, that means this specific contact does not have any name applied to it.

Also, unrelated and harmless, but it looks like you're specifying alt=json twice in the URL query params.

Blake O'Hare
  • 1,863
  • 12
  • 16
  • No that is also not returning name – Ishan Shah Aug 08 '16 at 06:29
  • Then there is no name on that particular contact. The Contacts API does not fall back to a default name value when one is not present and instead just omits the field. The name field was added in version 3 and up. Versions 1 and 2 use the field. If the title field is empty, then no name is present even if v=3 is enabled. I've updated the answer to be a little more specific. – Blake O'Hare Aug 09 '16 at 19:15
  • Yes actually you are right. there is no name in that field. but that contact is sync from **G+**. so what i need is if that name field is empty then it should show me the G+ name as shown in contact list. – Ishan Shah Aug 10 '16 at 05:20
  • With the exception of joining G+ profile photos onto your contact data, the Contacts API only returns the information that you yourself have defined directly in your contacts. If you want information the G+ user has defined in their profile, then you want to use the People API instead, which offers this functionality: https://developers.google.com/people/ – Blake O'Hare Aug 11 '16 at 17:39
  • YAh that i know. but i neeed to combine both into one. As if i used both of them then its taking long time to fetch the all users details. So i need to both at one place. – Ishan Shah Aug 16 '16 at 11:11
  • The People API does combine both contact and profile data into one response. – Blake O'Hare Aug 17 '16 at 03:46
0

we didn't find any field like first-name and last-name. any help would be appreciated

Getting the full name of your contact is a bit tricky. Using oauth playground, Contactsv3, here's how I did it:

  1. First, you need to know the contactId of your contact. How?

    GET https://www.google.com/m8/feeds/contacts/{your_userEmail}/full

    This URI request will return all contacts associated with your email. Now the contactId is found in XML <id> tag. <id>http://www.google.com/m8/feeds/contacts/../base/123456789abcdefg</id>

    In this case, 123456789abcdefg is the contactId of a certain person. The full name is found in the <title> tag.
    <title type="text">John Carmack</title>

  2. To fetch the person's contact details individually, unlike in Step 1, use: GET https://www.google.com/m8/feeds/contacts/{your_userEmail}/full/123456789abcdefg

It is now up to you to parse this response and use it accordingly. I haven't checked if there's a JSON format, but I hope this helps you.

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
  • Yah but that is also not returning any name – Ishan Shah Aug 08 '16 at 06:29
  • It returns a response body which contains the name "John Carmack" in the tag, what do you mean 'not returning any name'? You have to parse it. I just actually showed you. – ReyAnthonyRenacia Aug 08 '16 at 06:34
  • Ya actally it content null value in name colum. like > "title": { "type": "text", "$t": "" }, – Ishan Shah Aug 08 '16 at 08:14
  • The response body is in XML format not JSON. I haven't tried parsing XML formats yet. Try this [using jquery](http://stackoverflow.com/questions/510995/parse-xml-response-with-jquery). – ReyAnthonyRenacia Aug 08 '16 at 08:33
  • Ya i have also tried with XML but all are same as this JSON. – Ishan Shah Aug 08 '16 at 08:44
  • any error logs from the console? did you load the Contacts API library ? It is working in oauth playground, that means it works. So it's a matter of implementation. – ReyAnthonyRenacia Aug 08 '16 at 08:51
  • No, may be if i enter name manully then name got me. but if contect is sync from G+ the name is not feched by API – Ishan Shah Aug 08 '16 at 08:59
  • @noogui I also tried to fetch the contacts using OAuth playground but it doesn't showing any details about the name. When I manually add google contacts, I'm getting the Name field. But when I add contacts using OAuth playground I didn't get anything. I also tried copying the exact request body given in the Google Contact API. Can you tell me where I'm wrong? – Santhosh Jun 22 '18 at 05:12
  • I found where I'm wrong. I forgot to add the GData-Version in the header. Now I got the name field in the response . – Santhosh Jun 22 '18 at 05:17