2

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?

1 Answer 1

3

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

Authorization: Bearer your_token_here
Sign up to request clarification or add additional context in comments.

1 Comment

That was easy. Thanks alot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.