You can create a service wrapper around $http that takes care of OAuth-specific stuff (e.g. add a header, etc.) and use this service (instead of $http ) for OAuth-related requests.
Implementation Example:
app.factory('HttpOAuth', function ($http) { // Augments the request configuration object // with OAuth specific stuff (eg some header) function getAugmentedConfig(cfg) { var config = cfg || {}; config.headers = config.headers || {}; config.headers.someHeaderName = 'some-header-value'; return config; } // The service object to be returned (`$http` wrapper) var serviceObj = {}; // Create wrappers for methods WITHOUT data ['delete', 'get', 'head', 'jsonp'].forEach(function (method) { serviceObj[method] = function (url, config) { var config = getAugmentedConfig(config); return $http[method](url, config); }; }); // Create wrappers for methods WITH data ['post', 'put'].forEach(function (method) { serviceObj[method] = function (url, data, config) { var config = getAugmentedConfig(config); return $http[method](url, data, config); }; }); // Return the service object return serviceObj; });
See also this short demo .
source share