无法使用Google API获取联系人

时间:2022-12-04 15:47:32

I am using Google Contacts API v3 for extracting google contacts.

我使用Google Contacts API v3来提取谷歌联系人。

I'm started with the Google APIs Client Library for JavaScript for authentication and authorization. I have no problems with that part of the API access.

我开始使用适用于JavaScript的Google API客户端库进行身份验证和授权。我对API访问的那部分没有任何问题。

But after doing the auth part I have to fetch the google contacts(read only access would be fine for me). I am using gdata-javascript-client for the Google Contacts API v3. I am also referring google official doc and have copied the code and made the necessary changes to work for me.

但在执行身份验证部分后,我必须获取谷歌联系人(只读访问对我来说没问题)。我正在为Google Contacts API v3使用gdata-javascript-client。我也指谷歌官方文档,并已复制代码并进行必要的更改为我工作。

My problem is,

我的问题是,

  1. Its not working. Its not entering to the registered call back function.
  2. 它不起作用。它没有进入注册的回拨功能。

  3. I have also tried using read only mode. But that too is not working.
  4. 我也尝试过使用只读模式。但那也没有用。

There are two pieces of code that I am following, one for editable mode and other is read-only mode.

我正在遵循两段代码,一条用于可编辑模式,另一条用于只读模式。

Editable mode access:

可编辑模式访问:

function handleAuthResult(authResult){
    if (authResult && !authResult.error) {
        fetch_contacts_data(authResult);
    };
}

function auth() {
    var config = {
        'client_id': 'CLIENT_ID',
        'scope': 'https://www.google.com/m8/feeds'
    };
    gapi.auth.authorize(config, handleAuthResult);
}


function fetch_contacts_data(token) {
    $.ajax({
        url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token  + "&max-results=70000&alt=json" + "&callback=?",
        dataType: "json",
        success:function(data) {
            contacts = [];
            for (var i = 0, entry; entry = data.feed.entry[i]; i++) {
                var contact = {
                    'name' : entry['title']['$t'],
                    'id' : entry['id']['$t'],
                    'emails' : [],
                    'phoneNumber' : []
                };

                if (entry['gd$email']) {
                    var emails = entry['gd$email'];
                    for (var j = 0, email; email = emails[j]; j++) {
                        contact['emails'].push(email['address']);
                    }
                }
                if (entry['gd$phoneNumber']) {
                    var phoneNumber = entry['gd$phoneNumber'];
                    for (var j = 0, phone; phone = phoneNumber[j]; j++) {
                        contact['phoneNumber'].push(phone['$t']);

                    }
                }
                if (!contact['name']) {
                    contact['name'] = contact['emails'][0] || "<Unknown>";
                }
                contacts.push(contact);
            }
            numContacts = contacts.length;
            friend_list_json_str = '';
            for(var j=0;j<numContacts;j++) {
                name = (contacts[j])['name'];
                emails = (contacts[j])['emails'];
                phone = (contacts[j])['phoneNumber'];
                email_list= '';
                phone_list= '';
                for(var k=0;k<emails.length;k++) {
                    email_list += '"'+emails[k] + '",' ;
                }
                email_list = email_list.substring(0, email_list.length -1)

                for(var k=0;k<phone.length;k++) {
                    phone_list = '"'+phone[k] + '",';
                }
                phone_list += phone_list.substring(0, phone_list.length -1)

                friend_json_str = '';
                friend_json_str += '{"name":"'+name + '",';
                friend_json_str += '"emails":['+email_list+'],';
                friend_json_str += '"phoneNumber":['+phone_list+']' ;
                friend_json_str += '},';
                friend_list_json_str += friend_json_str;
            }
            friend_list_json_str = friend_list_json_str.substring(0, friend_list_json_str.length - 1);

            var user_data = get_user_data();
            var len = user_data.length;
            user_data = user_data.substring(0, len - 2);
            user_data += friend_list_json_str + ']}';
            data = "invite_data="+ user_data;
            url = '/invite';
            var posting = $.post( url, data );

            posting.done(function( response_data ) {
            });
        }
    });
}

Read Only access:

只读访问权限:

function auth() {
    var config = {
        'client_id': 'CLIENT_ID',
        'scope': 'https://www.googleapis.com/auth/contacts.readonly'
        };
    gapi.auth.authorize(config, handleAuthResult);
}

NOTE: rest of the code is same as above

注意:其余代码与上面相同

In both the cases the ajax call is failing,

在这两种情况下,ajax调用失败,

 $.ajax({
            url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token  + "&max-results=70000&alt=json" + "&callback=?",
            dataType: "json",
            success:function(data)

Can someone please tell me why this code is not working?

有人可以告诉我为什么这段代码不起作用?

1 个解决方案

#1


You're blocked by your browser's popup blocker.

您被浏览器的弹出窗口拦截器阻止了。

Try to call the auth() method after clicking on a button, it should work.

尝试在单击按钮后调用auth()方法,它应该工作。

To void this issue, you need to :

要解决此问题,您需要:

  1. First, attempt a call to gapi.auth.authorize with the parameter {immediate:true}. It will try to obtain a token in background if the user already gave his permission.
  2. 首先,尝试使用参数{immediate:true}调用gapi.auth.authorize。如果用户已经允许,它将尝试在后台获取令牌。

  3. If it fails, display a button to the user for authentication. When the user clicks on it, call gapi.auth.authorize with the parameter {immediate:false}.
  4. 如果失败,则向用户显示一个按钮以进行身份​​验证。当用户单击它时,使用参数{immediate:false}调用gapi.auth.authorize。

#1


You're blocked by your browser's popup blocker.

您被浏览器的弹出窗口拦截器阻止了。

Try to call the auth() method after clicking on a button, it should work.

尝试在单击按钮后调用auth()方法,它应该工作。

To void this issue, you need to :

要解决此问题,您需要:

  1. First, attempt a call to gapi.auth.authorize with the parameter {immediate:true}. It will try to obtain a token in background if the user already gave his permission.
  2. 首先,尝试使用参数{immediate:true}调用gapi.auth.authorize。如果用户已经允许,它将尝试在后台获取令牌。

  3. If it fails, display a button to the user for authentication. When the user clicks on it, call gapi.auth.authorize with the parameter {immediate:false}.
  4. 如果失败,则向用户显示一个按钮以进行身份​​验证。当用户单击它时,使用参数{immediate:false}调用gapi.auth.authorize。