使用Javascript中的Google People API获取联系电子邮件地址

时间:2016-03-01 08:07:20

标签: javascript email google-api google-people

我想检索用户的联系人的电子邮件地址。 我打算使用Google People API。 这是我要检索的功能:

function makeApiCall() {
    gapi.client.load('people', 'v1').then(function() {
        var request = gapi.client.people.people.connections.list({
            resourceName: 'people/me',
            pageSize: 200,
            fields: 'connections(resourceName)'
        });
        request.execute(function(resp) {
            if (resp.connections){
                var batch = gapi.client.newBatch(); 
                var emails = [];
                for(var i = 0; i < resp.connections.length; i++){
                    var req = gapi.client.people.people.get({
                        resourceName: resp.connections[i].resourceName,
                        fields: "emailAddresses(value)"
                    });
                    batch.add(req);
                    req.then(function (email){
                        var idx = email.body.indexOf("{");
                        var jsonString = email.body.substring(idx > 0 ? idx : 0); 
                        var obj;
                        try {
                            obj = JSON.parse(jsonString);
                            if (obj.emailAddresses){ 
                                for (j = 0; j < obj.emailAddresses.length; j++){ 
                                    emails.push(obj.emailAddresses[j].value);
                                } 
                            }
                        } catch (err) {
                            console.error(email);
                        }
                    })
                }
                batch.then(function (){
                    console.log(emails); 
                }, function (err){
                    console.error(err);
                }); 
            } else {
                console.error("Error", resp);
            }
        });
    }, function (err){
        console.error(err);
    });
}

基本上,我提取200个连接来检索他们的resourceName 此reresourceName将用于获取用户的电子邮件地址 我正在使用批量请求来获取200个用户的电子邮件。

问题是,对于某些用户,我收到以下错误:

{
  "error": {
    "code": 429,
    "message": "Insufficient tokens for quota group and limit ReadGroupUSER-100s using the limit by ID 991457241470@515501272068.",
    "status": "RESOURCE_EXHAUSTED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developer console API key",
            "url": "https://console.developers.google.com/project/991457241470/apiui/credential"
          }
        ]
      }
    ]
  }
}

可能是什么原因以及如何解决此问题?

谢谢

1 个答案:

答案 0 :(得分:1)

最后,我得到了解决方案。它使用Google API登录,并使用AJAX调用来获取联系人详细信息。

function getEmailList($scope, callback, fetchBeginCallback) {
    _googleLogin(AppConstants.GOOGLE_API.scopes.contactEmailListing, function(err) {
        if (err) {
            $scope.$apply(function() {
                callback(err);
            });
        } else {
            fetchBeginCallback ? fetchBeginCallback() : $.noop();
            var emails = [];
            $.ajax({
                    url: "https://www.google.com/m8/feeds/contacts/default/full?access_token=" + gapi.auth.getToken().access_token + "&alt=json&max-results=1000",
                    dataType: 'jsonp'
            }).done(function(data) {
                var entries = data.feed.entry;
                for (var i = 0; i < entries.length; i++) {
                    if (entries[i].gd$email) {
                        if (emails.indexOf(entries[i].gd$email[0].address) < 0) {
                            emails.push(entries[i].gd$email[0].address);
                        }
                    }
                }
                callback(undefined, emails);
            }).fail(function(jqXHR, textStatus, errorThrown) {
                callback(errorThrown, emails);
            });
        }
    });
}

function _googleLogin(scope, callback) {
    gapi.auth.authorize({
            client_id: AppConstants.GOOGLE_API.clientId,
            scope: scope,
            immediate: false
    }, function handleAuthResult(authResult) {
        if (authResult && !authResult.error) {
            callback();
        } else {
            callback(authResult ? authResult.error : "Empty Response");
        }
    });
}