【问题标题】:How to make Google Contact API work with AngularJS?如何使 Google Contact API 与 AngularJS 一起工作?
【发布时间】:2017-06-07 05:01:25
【问题描述】:

我一直在尝试从响应中获取数据并将其添加到控制器,但没有运气。

这是我的previous question

在 AngularJS 中是如何实现的?

【问题讨论】:

    标签: javascript angularjs ionic-framework google-contacts-api


    【解决方案1】:

    我使用的代码是这样的:

    invitePeersController.getGmailContacts = function(){
        console.log("I come in gmail contacts");
        var clientId = "contact key";
        var apiKey = "apiKey";
        var scopes = "https://www.googleapis.com/auth/contacts.readonly";
        authorize();
        function authorize() {
            gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);
        }
        function handleAuthorization(authorizationResult){
            invitePeersController.gmailContacts = [];
            var gmailData = [];
            if (authorizationResult && !authorizationResult.error){
                var urlContact = "https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=50000&v=3.0";
                var promiseGoogleData = HttpService.httpGetExternalLink(urlContact);
                promiseGoogleData.then(function (response) {
                    var jsonChildData = response.data.feed.entry;
                    for(var i=0; i<jsonChildData.length ;i++){
                        var item = {};
                        try{
                            var name = jsonChildData[i].title.$t;
                            var email = jsonChildData[i].gd$email[0].address;
    
                            if(name.substring(1, name.length-1) && email.substring(1, email.length-1)){
                                item ["name"] = name.substring(1, name.length-1);
                                item ["email"] = email.substring(1, email.length-1);
                                item ["id"] =  email.substring(1, email.length-1).replace(/[^a-zA-Z ]/g, "");
                            invitePeersController.gmailContacts.push(item);
                            gmailData.push(item);
                           }
    
                        }catch(error){
                            console.log("Error is thrown while trying to read gmail resposne");
                    }
    
                }
                $state.go("app.inviteContacts");
                InvitePeersService.setGmailContactsData( invitePeersController.gmailContacts);
    
                            return response;
                        })
                        .catch(function (error) {
                            console.log("Something went terribly wrong while trying to get Gmail Data.");
                        });
                    }
    
    
                }
    
            }
    

    另外不要忘记在凭证中添加域名:

    【讨论】:

      猜你喜欢
      • 2015-07-31
      • 2019-05-26
      • 2012-05-20
      • 2012-10-12
      • 2014-05-12
      • 1970-01-01
      • 2018-05-25
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多