![](/img/trans.png)
[英]How to fix refresh access token from the first http request which gives GET 401 error
[英]How refresh token works and how last failed http request called again which gave 401…?
我正在使用angularjs
使用OAuth2
。 現在,我無法使用OAuth
進行身份驗證,因為我無法重新發送最后一個401 API。 任何想法。
我正在使用此 oauth2存儲庫。
Controller.js
app.controller('validate', ['$scope', '$rootScope', '$location', 'fullname', '$http', '$timeout', '$cookies', 'OAuth', function ($scope, $rootScope, $location, fullname, $http, $timeout, $cookies, OAuth) {
OAuth.getAccessToken($scope.user).then( function successCallBack(response){
$scope.response = response;
if($scope.response.status == 200){
console.log($scope.response.data);
$scope.accessToken = $scope.response.data.access_token;
$scope.refreshToken = $scope.response.data.refresh_token;
localStorage.setItem("accessToken", $scope.accessToken);
localStorage.setItem("refreshToken", $scope.refreshToken);
var userId = response.headers('userid');
console.log(userId);
$cookies.put("userId", userId);
window.location.href = 'user_profile.php';
}
}, function errorCallBack(response){
console.log(response);
});
}]);
app.js
app.config(['OAuthProvider', function(OAuthProvider) {
OAuthProvider.configure({
baseUrl: 'http://testzone.xxxxxx.net/api/LoginTest/Login/web/',
clientId: '123456789',
clientSecret: 'otszh9nonaosok88gsswc8k4w8ww04s',
grantPath: 'api/oauth2/token',
revokePath: 'api/oauth2/revoke'
});
}]);
app.run(['$rootScope', '$window', 'OAuth', '$cookies', '$timeout', function($rootScope, $window, OAuth, $cookies, $timeout) {
$rootScope.$on('oauth:error', function(event, rejection) {
// Ignore `invalid_grant` error - should be catched on `LoginController`.
if ('invalid_token' === rejection.data.error || 'invalid_grant' === rejection.data.error || 'invalid_request' === rejection.data.error || 'invalid_client' === rejection.data.error || 'unauthorized_client' === rejection.data.error || 'unsupported_grant_type' === rejection.data.error) {
$cookies.remove('userId');
$timeout(function(){
window.location.href = 'index.php';
},200);
}
// Refresh token when a `invalid_token` error occurs.
if ('expired_token' === rejection.data.error) {
console.log(rejection);
OAuth.getRefreshToken();
}
console.log(rejection);
console.log(rejection.data.error);
console.log(rejection.data.error_description);
// Redirect to `/login` with the `error_reason`.
//return $window.location.href = 'index.php';
});
}]);
謝謝
分析錯誤響應時,您可以執行以下操作:
if (rejection.status === 401) {
var authService = $injector.get('oAuthService');
var authData = ipCookie(oAuthConstants.oAuthCookieName);
var $http = $http || $injector.get('$http');
var deferred = $q.defer();
if (authData) {
authService.refreshToken().then(function () {
//this repeats the request with the original parameters
return deferred.resolve($http(rejection.config));
});
}
return deferred.promise;
}
else if (rejection.status === 403) {
var toaster = $injector.get('toaster');
toaster.pop('error', "Access Denied", "You are not authorized to do this request.");
}
else {
return $q.reject(rejection);
}
重復上一個401 API調用的關鍵是:
return deferred.resolve($http(rejection.config));
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.