1

我想检索用户联系人的电子邮件地址。我打算使用 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 个连接来检索它们的资源名称。
此 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"
          }
        ]
      }
    ]
  }
}

可能是什么原因,我该如何解决这个问题?

谢谢

4

1 回答 1

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");
        }
    });
}
于 2016-05-24T03:40:38.753 回答