Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am new to angularjs. I am trying to make an API request that requires authorization. I have included it in the header of the request, but it is still not working. I am sure my access token is working. Any advice?

$scope.fetch = function() {
$scope.code = null;
$scope.response = null;
$http({method: $scope.method, url: $scope.url, cache: $templateCache, headers:{Authorization:"access token"}}).
  success(function(data, status) {
    $scope.status = status;
    $scope.data = data;
  }).
  error(function(data, status) {
    $scope.data = data || "Request failed";
    $scope.status = status;
});

};

share|improve this question
    
Add "Basic " before access token. Possible duplicate of this one here stackoverflow.com/questions/11876777/… –  DevPat Oct 18 '13 at 21:20
add comment

2 Answers

You could use following

 $http.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=utf-8";

It will add the above header to every POST call you make from your app. For adding a header common to all method, try following.

$http.defaults.headers.common['Authorization'] = "Bearer " + user.oauthInfo.access_token;
share|improve this answer
    
Where would you put that line of code? –  Squadrons Mar 21 at 3:10
    
Anywhere...Best way to put this line in Config part of your code OR even you can create a request interceptor to set header –  vs4vijay Mar 21 at 7:19
add comment

Do you see the header in your browser's network request log for the Request?

If so, is it in the expected format? Typically the "Authorization" header will have something before it, like "Basic " (as DevPat mentions in a comment above) or "Bearer ". What belongs here is dependent on the backend system receiving the request.

Examples of expected header:

Authorization: Bearer access_token
Authorization: Basic access_token
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.