title: Angularjs $http service passing facebook access token
body: <p>I'm implementing fb authentication in my SPA built using MEAN stack. While I've successfully implemented the fb authentication using facebook token passport strategy, I'm facing issues in securing API endpoints. Because for that I need to pass both the authenticated user object and access token in the $http service and I've tried passing access_token as a property of the user object and also as a header property, but I still 401 (Unauthorized error). Below is my code snippet.</p>
  
  <p>Passport documentation says "Authorization: Bearer base64_access_token_string". Should the token be encoded in a base64 format? Pls help.</p>
  
  <p><strong>server code</strong></p>
  
  <pre><code>app.get('/api/getbikes*',
  passport.authenticate('facebook-token',{session: false}),
  function(req,res){
      if(req.user){
          console.log('In getbikes api');
      // console.log('req.query :',req.query);
          var msg="";
          ubBike
              .find({cust:req.query._id})
              .populate('cust','email')
              .exec(function(err,bikes){
                  res.send(bikes);
                  if(err) throw err;
              });
      }
      else
      {
          res.send(401);
      }
  
  });
  </code></pre>
  
  <p><strong>angular code</strong></p>
  
  <p><em>service</em></p>
  
  <pre><code>this.getbikes = function(user){
      var deferred = $q.defer();
      $http({
          method:"GET",
          url:"http://localhost:3000/api/getbikes",
          params: user,
          headers:{
              Authorization:auth.getAccesstoken()
          }            
      }).then(function successCallback(srresponse){
          deferred.resolve(srresponse.data);
      }, 
          function failureCallback(srresponse){
          $log.error("get bikes http call failed ",srresponse.data);
          deferred.reject(srresponse.data);
      });//$http
      return deferred.promise;
  };//getbikes
  </code></pre>
  
  <p><em>controller</em></p>
  
  <pre><code>$scope.fblogin= function(){
          auth.fblogin().then(
                  function(response){
  
                  $scope.isAuth = auth.isAuth;
                  $scope.usr =auth.getResponseobj();
                  $scope.usr.access_token=auth.getAccesstoken();  
                  $scope.profpic=auth.profpic;
  
                  bike.getbikes($scope.usr).then(function(response){
  
                      if (response.length ==0)
                      {
                      $location.path('/addbike');//redirect to addbike screen    
                      }
                      else{
                      $location.path('/appoint');//else redirect to view appointment screen
                      }
                  },function(reason){
                      $scope.msg1 = reason;
                  });//getbikes
  
  
              },function(reason){
                   $log.log("fblogin() - failure :Need to login to the application :"+reason);
              })
  
          };//fblogin
  </code></pre>