这似乎是一个简单的案例:我正在尝试添加一个请求拦截器,以使用 Angular 1.1.5 将 OAuth 不记名令牌添加到 $http 请求。这是我的拦截器:
app.factory('bearerTokenInterceptor', function($injector, $q) {
/*
* This interceptor is available for providers that use the header based
* bearer token for authentication
*/
return {
request: function(config) {
/*
* We need to use $injector to get access to the Token provider within
* the body of the ctor - lest we want circular references created
* by providers that need to use the interceptor (and also need the
* Token provider
*/
var Token = $injector.get('Token');
config.headers.get = {'Authorization': 'Bearer ' + Token.get() };
return config || $q.when(config);
}
};
});
接着
app.config(function(TokenProvider, YammerTokenVerifier, $httpProvider) {
TokenProvider.extendConfig({
authorizationEndpoint: 'https://www.yammer.com/dialog/oauth',
verifyFunc: YammerTokenVerifier
});
/*
* Yammer uses a bearer token - in comes the BearerTokenInterceptor!
*/
$httpProvider.interceptors.push('bearerTokenInterceptor');
});
但是当我实际上然后使用 $http
$http.get('https://www.yammer.com/api/v1/users/current.json')
.success(function(data) {
deferred.resolve(data);
})
.error(function(data, status, headers, config) {
console.log(data);
console.log(status);
console.log(headers);
console.log(config);
deferred.reject(data);
});
请求拦截器没有被调用。我没有看到我正在做的任何事情超出了 Angular 文档的建议。我错过了什么?