3

I'm trying to set a header to send an access token on every request. This is what I have tried:

.factory("TokenRestangular", "StorageService", function (Restangular, StorageService) {
    return Restangular.withConfig(function (RestangularConfigurer) {


        // Set access token in header.
        RestangularConfigurer.setDefaultHeaders({Authorization:'Bearer '+ StorageService.get("access_token")});
        RestangularConfigurer.setBaseUrl('/api');
    });
})

I get the error:

Error: [ng:areq] http://errors.angularjs.org/1.2.19/ng/areq?p0=fn&p1=not%20aNaNunction%2C%20got%string
    at Error (native)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:6:450
    at Bb (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:19:68)
    at Ua (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:19:155)
    at rc (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:32:423)
    at Object.d [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:34:398)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:36:288
    at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:34:305)
    at d (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:35:6)
    at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js:35:165) 

1 Answer 1

3

I think your factory function has one too many parameter passed, try this:

    .factory("TokenRestangular", function (Restangular, StorageService) {
        return Restangular.withConfig(function (RestangularConfigurer) {
        // Set access token in header.
        RestangularConfigurer.setDefaultHeaders({Authorization:'Bearer '+ StorageService.get("access_token")});
        RestangularConfigurer.setBaseUrl('/api');
    });
})

or

    .factory("TokenRestangular", ["Restangular", "StorageService", function (Restangular, StorageService) {
        return Restangular.withConfig(function (RestangularConfigurer) {
        // Set access token in header.
        RestangularConfigurer.setDefaultHeaders({Authorization:'Bearer '+ StorageService.get("access_token")});
        RestangularConfigurer.setBaseUrl('/api');
    });
}])

if you want to have safe minification code.

Sign up to request clarification or add additional context in comments.

3 Comments

My problem is I will refresh the token over time, I successfully get the new token but when I run another RESTful API the token it uses is still the old one.
@aokaddaoc I dealing with the same problem.. did you solve it ?
@bitmind Yes I do. jsfiddle.net/hcq8sx7p/1 Hope it can troubleshoot your problem~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.