简体   繁体   English

在javascript中通过google登录获取所有用户的联系人列表(包括电话号码)

[英]get all user's contact list (including phone number) from google login in javascript

I am trying to get the contact number of all the user's contacts through google sign in. I am getting contact name, email and other details but not the contact number. 我正在尝试通过Google登录获取所有用户联系人的联系电话。我正在获取联系人姓名,电子邮件和其他详细信息,但没有联系电话。 I am specifically trying contacts API v3. 我专门尝试使用联系人API v3。

this official link 这个官方链接

gives a way to get contact details (including phone number) but I am not really sure how to do that in javascript. 提供了一种获取联系方式(包括电话号码)的方法,但是我不确定如何在javascript中做到这一点。 I googled around and found that people are using contacts API v2 and v1 to get this information. 我四处搜寻,发现人们正在使用通讯录API v2和v1来获取此信息。 But even there I couldn't find any suitable example. 但即使在那儿,我也找不到任何合适的例子。 My current implementation using google api (gapi) gives me contacts in this format 我目前使用google api(gapi)的实现以这种格式为我提供了联系方式

displayName: "Jamie Lannister" etag: ""XXXXXXXX-YYYYYYYYYYYYYYYYYY/Wl9XXXXXXXXXXXXXZ9-cJsx-wUY"" id: "107108760XXXXXXXXXXXX" image: Object url: " https://lh4.googleusercontent.com/-9Iyxxxxxxx/xxxxxxxxxxxxxxx/xxxxxxxxxxk/xxxxxx-xxx/photo.jpg?sz=50 " proto : Object kind: "plus#person" objectType: "person" url: " https://plus.google.com/107108760XXXXXXXXXXXX displayName:“ Jamie Lannister” etag:“” XXXXXXXX-YYYYYYYYYYYYYYYYYY / Wl9XXXXXXXXXXXXXXXXZ9-cJsx-wUY“” id:“ 107108760XXXXXXXXXXXX”图片:对象url:“ https://lh4.googleusercontent.com/-9Iyxxxxxxx/xxxxxxxxxkxxx/xxxx -xxx / photo.jpg?sz = 50proto :对象种类:” plus#person“ objectType:” person“网址:” https://plus.google.com/107108760XXXXXXXXXXXX

I got this from this link and this link . 我从此链接和此链接获得此信息。

My question is it really possible to get a user's google+ or google contacts with phone numbers? 我的问题是,是否真的有可能获得带有电话号码的用户的google +或google联系人? If yes please answer here. 如果是,请在这里回答。 Is there any other way to do this? 还有其他方法吗?

Oh by the way, this question is not eligible for bounty yet. 哦,顺便说一句,这个问题还没有资格获得赏金。 Correct answer gets +50 bounty in next 2 days. 正确答案将在接下来的2天内获得+50悬赏。

I used the following javascript function (httpGet) to get the google contacts with name and number. 我使用以下javascript函数(httpGet)来获取具有名称和号码的google联系人。 The url i passed in the "theUrl" param is : " https://www.google.com/m8/feeds/contacts/user.email@gmail.com/full?access_token= "+authToken+'&max-results=9999'" 我在“ theUrl”参数中传递的网址是:“ https://www.google.com/m8/feeds/contacts/user.email@gmail.com/full?access_token= “ + authToken +'&max-results = 9999 “”

function httpGet(theUrl)
    {
        $.ajax({
            type: "GET",
            url: theUrl,
            dataType: "jsonp",
            success: function (xml) {               
                $(xml).find('entry').each(function(){
                    var name = '';
                    var number = '';
                    $(this).find("gd\\:phoneNumber").each(function(){
                        number = $(this).text();
                    });
                    $(this).find("title").each(function(){
                        name = $(this).text();
                    });

                    if(number!=null && number.length>0)
                    {
                        number = number.replace(/[^0-9]/g,'');
                        userContacts.push(name+'-'+number);
                    }
                });


            },
        });
    }

Explanation: by default google returns a particular number of contacts, to get all the contacts pass a really big number in max-results parameter (hence the 9999). 说明:默认情况下,Google返回特定数量的联系人,以使所有联系人在max-results参数中传递一个很大的数字(因此为9999)。 authToken is what you get after you authorize the user. authToken是授权用户后得到的。 email address: after getting authtoken you an query for email address as well. 电子邮件地址:获得身份验证令牌后,您也会查询电子邮件地址。 here is how i did it: 这是我的做法:

function getEmail(){
        gapi.client.load('oauth2', 'v2', function() {
            var request = gapi.client.oauth2.userinfo.get();
            request.execute(getEmailCallback);
        });
    }

function getEmailCallback(obj){
        email = '';
        if (obj['email']) {
            email = obj['email'];
            //here you get the email address of the user who just signed in
        }
    }

the gapi here is google's api javascript object. 此处的gapi是Google的api javascript对象。 add this script and you are good to go 添加此脚本,您就可以开始了

script type="text/javascript" src="http://www.google.com/jsapi"/ (add jQuery script too)

Without the ajax call in httpGet(), you will receive a cross domain compliance error. 没有在httpGet()中进行ajax调用,您将收到跨域合规性错误。 What you receive after httpGet of the mentioned url is an xml file, the whole success lot inside the httpGet() method is for parsing that xml, you will also receive a bunch of other info, if required parse those too (I only needed contact number and name). 在提到的URL的httpGet之后收到的是一个xml文件,httpGet()方法内部的全部成功都是用于解析该xml,您还将收到一堆其他信息,如果需要也解析这些信息(我只需要联系数字和名称)。 if you want a google account with contacts just log in to google contacts and add some test phone numbers there. 如果您想使用带有联系人的Google帐户,只需登录到Google联系人并在其中添加一些测试电话号码即可。

Needless to say you will also need client Id, secret, API key etc for this. 不用说,您还将为此需要客户端ID,密钥,API密钥等。 Please go to the link provided below for details. 请转到下面提供的链接以获取详细信息。 the user contacts var in httpGet() contains all the user contacts separated by hyphen. httpGet()中的用户联系人var包含所有用连字符分隔的用户联系人。 The regex pattern in httpGet() is to pick only numbers from contact number or else you might get something like this (123) 456 7890. httpGet()中的正则表达式模式是仅从联系电话中选择号码,否则您可能会收到类似这样的信息(123)456 7890。

more details straight from google 直接从Google获得更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM