這看起來很簡單:我試圖添加一個請求攔截器,使用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的文檔所暗示的。我錯過了什麼?
你不應該注入$注入器並調用get注入器。爲什麼不直接請求'Token'而不是'$ injector'? – ganaraj
當我以標準方式進行操作時,庫中的模式會導致循環引用。這是打破它的唯一方法。 –