3

I'am trying to import a user's gmail contacts using Angular Js. The code is working fine in plain javascript but giving error in angular js.

HTML Code..

<a class="btn btn-primary btn-simple" ng-click="importgoogle()"><u>Import Gmail Friends</u></a>

Angular Code..

   var clientId = 'Client ID';
var scopes = 'https://www.googleapis.com/auth/contacts.readonly';
   $scope.importgoogle = function(){
    window.setTimeout(authorize);       //calls authorize()
}

var authorize = function(){
    gapi.auth.authorize({client_id: clientId, scope: scopes, immediate: false}, handleAuthorization);       //calls handleAuthorization()
}

var handleAuthorization = function(){
    if (authorizationResult && !authorizationResult.error) {
            $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0",
            function(response){
                console.log(response);
                });
        }
}

After entering a user's Id & password the following error message is displayed in console..

  Uncaught ReferenceError: authorizationResult is not defined

Can't understand where I'm going wrong as this code is working in Javascript.Please help..

Lucy
  • 1,812
  • 15
  • 55
  • 93

2 Answers2

2

Here is the working example using Angular Js:

app.controller("importGCCtrl", function($scope, $http) {
$scope.config = {
    'client_id': 'Client ID',
    'scope': 'https://www.google.com/m8/feeds'
};

$scope.inviteContacts = function() {
    gapi.auth.authorize($scope.config, function() {
        $scope.fetch(gapi.auth.getToken());
    });
}

$scope.fetch = function(token) {
    $http.get("https://www.google.com/m8/feeds/contacts/default/full?access_token=" + token.access_token + "&alt=json").then(function(response) {
        console.log(response);
        //console.log(response.data.feed.entry);
        //$scope.contacts = response.data.feed.entry; // to assign data
    });
}

});

*NOTE: Please make sure you have included the API - <script src="https://apis.google.com/js/client.js"></script> on the page

senvikal
  • 110
  • 2
  • 10
0

The problem lies in the handleAuthorization function. The correct way of implementing that function is..

 var handleAuthorization = function(authorizationResult){         //authorizationResult needs to be passed as an arguement.
if (authorizationResult && !authorizationResult.error) {
        $.get("https://www.google.com/m8/feeds/contacts/default/thin?alt=json&access_token=" + authorizationResult.access_token + "&max-results=500&v=3.0",
        function(response){
            console.log(response);
            });
    }

}

After making this change the Angular Js code is now working properly.

Lucy
  • 1,812
  • 15
  • 55
  • 93