Right now, the only way that I know for setting tokens in headers dynamically for an angularjs call is via $http
like so:
return $http.get({
url: 'https://my.backend.com/api/jokes',
params: {
'jokeId': '5',
},
headers: {
'Authorization': 'Bearer '+ $scope.myOAuthToken
}
});
But I want to figure out how to pass this via $resource, here's some pseudo-code that doesn't work:
...
.factory('myFactory',
['$resource',
function($resource){
return {
jokes: $resource('https://my.backend.com/api/jokes', null, {
query: {
method: 'GET'
}
})
};
}
]
);
...
return myFactory.jokes.query({
'jokeId': '5',
'headers': {
'Authorization': 'Bearer '+ $scope.myOAuthToken
}
});
How can I pass headers on the fly to $resource for angularjs?
sample.factory('Restricted', ['$resource', function($resource){ return { createRestrictedResource: function(token) { return $resource('/api/v1/restricted', null, {query: {headers: {'Authorization': 'Bearer ' + token}}}); } } }]);
– Kindhearted