Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I try to make authenticated request to a WebApi server, using angular.

This is how I make the request:

var config = {headers: {Authorization: "Bearer " + JSON.parse($cookies.token)}};
var url = 'http://localhost:53889/api/Values/5';

$http.get(url,config).success(function(response){
     console.log(response);
})

And this is how I set the authentication:

login: function(credentials){
          return $http({
             method: 'POST',
             url: url + '/Token',
             headers: {'Content-Type' : 'application/x-www.form-urlencoded'},
             transformRequest : function(obj){
                 var str = [];
                 for(var p in obj){
                   str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                 }
                 return str.join("&");
             },
               data: credentials
        });

var success = function(data){
    AuthorizationFactory.setCurrentUser(credentials.username);
    ApiFactory.init(data["access_token"]);
    $cookieStore.put('token', JSON.stringify(data["access_token"]));
};

The success method is called like this: AuthorizationFactory.login(credentials).success(success).

But everytime I try to make a authenticated request, I get a 401 error message.

The headers look like this:

Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:ro-RO,ro;q=0.8,en-US;q=0.6,en;q=0.4
Authorization:Bearer "4N_9ScOCtFxTgNs....t"
Cache-Control:no-cache
Connection:keep-alive
Cookie:.AspNet.Cookies=Rx...y0OTHrvE
Host:localhost:53889
Pragma:no-cache
Referer:http://localhost:53889/app/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36
X-Access-Token:"\"4N_9ScO...XZ6t\"" 

What am I doing wrong? What I'm missing?

share|improve this question

Do not use quotation marks in the Authorization header. It should look like this:

Authorization: Bearer your_token_here
share|improve this answer
1  
That was easy. Thanks alot! – CrazyDog Feb 5 '15 at 9:02

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.