EDIT : i get an error like this when i last checked in browser console.

> 
> TypeError: Cannot read property 'defer' of undefined
> 
> 
> 

I need to call one $http request which gives the token that can be used to call another $http request and finally the required response.
Hence i am using promises to make it work synchronously.However the function does not get executed after the `$q.defer()` function
Following is my code:

```
 $scope.firstTimeAuth = function($q) {

    var deferred = $q.defer();
    var ref = window.open('https://accounts.google.com/o/oauth2/auth?client_id=' + clientId + '&redirect_uri=http://localhost/callback&scope=https://www.googleapis.com/auth/fitness.activity.write &approval_prompt=force&response_type=code&access_type=offline', '_blank', 'location=no');
    ref.addEventListener('loadstart', function(event) {

        if((event.url).startsWith("http://localhost/callback")) {
            requestToken = (event.url).split("code=")[1];

            $http({
                method: "post", url: "https://accounts.google.com/o/oauth2/token",
                data: "client_id=" + clientId + "&client_secret=" + clientSecret + "&redirect_uri=http://localhost/callback" + "&grant_type=authorization_code" + "&code=" + requestToken
              })
              .success(function(data) {
                      defer.resolve(true);
                      accessToken = data.access_token;
                      refreshToken = data.refresh_token;
                      alert("firstTimeAuth success");

                      if(typeof(Storage) != "undefined") {
                          localStorage.setItem("refreshToken",refreshToken);
                          alert(localStorage.getItem("refreshToken"));
                      } else {
                          alert("Sorry, your browser does not support Web Storage...");
                      }

                      //functions here
                  })
               .error(function(data, status) {
                      alert("ERROR: " + data);
                      defer.resolve(true);
                  });
              ref.close();
          }
      });
     return deferred.promise;
  }

```

> 
> This is my second function
> 
> 
> 

```
$scope.getAcessToken = function($q)
  {
    var deferred = $q.defer();
    alert("inside getAcessToken function");
    refreshToken = localStorage.getItem("refreshToken");

    if(refreshToken)
    {
      $http({
            method: "post", url: "https://accounts.google.com/o/oauth2/token",
            data: "client_secret=" + clientSecret + "&grant_type=refresh_token" + "&refresh_token="+ refreshToken + "&client_id=" + clientId
              })
      .success(function(data){
            accessToken = data.access_token;
            alert("getAcessToken success" + accessToken);
            deferred.resolve(true);
      })
      .error(function(data,status){
        alert("ERROR: " + JSON.stringify(data) + status);
        deferred.resolve(true);
      });
    }
    else
    {
      $scope.firstTimeAuth();
    }
   return deferred.promise;
  } 

```

> 
> and i call them like this.
> 
> 
> 

```
 alert("not installed");
      var lastSaved = $scope.getFirstEpochTime();

      //walkthroug
      //Registe

      $scope.firstTimeAuth().then(function(){
        alert("firstime done");
        $scope.getDataSets().then(function(){
          alert(" in the last block");/*
          $scope.handleResponse().then(function(){
            $scope.insert().then(function(){
                $scope.select();
              })
            alert("done in installed");
          })
        */})
      })

```

Please let me know what is wrong with the code. i am very new to this.. thanks.