【发布时间】:2016-09-11 05:20:28
【问题描述】:
我有以下控制器:
angular.
module('phoneList').
component('phoneList', {
templateUrl: '/phone-list.template.html',
controller: ['$http',
function PhoneListController($http) {
var self = this;
var access_token = '';
var data = $.param({
grant_type: 'password',
username: 'test',
password: 'test',
client_id:'1234',
client_secret:'12345',
});
var config = {
headers : {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
}
}
$http.post('/o/token/', data, config)
.success(function (data, status, headers, config) {
self.access_token = data['access_token'];
console.log(access_token);
})
.error(function (data, status, header, config) {
$scope.ResponseDetails = "Data: " + data +
"<hr />status: " + status +
"<hr />headers: " + header +
"<hr />config: " + config;
});
var header = {
headers : {
'Authorization': 'Bearer '+self.access_token
}
}
$http({method: 'GET', url: 'api/posts/', headers: {'Authorization': 'Bearer '+self.access_token}}).then(function(response) {
self.phones = response.data;
});
}
]
});
我想使用从该函数返回的持续数天的访问令牌。我不想每次都获取新令牌,但要确定令牌是否已过期或是否需要检索另一个令牌:
$http.post('/o/token/', data, config)
.success(function (data, status, headers, config) {
self.access_token = data['access_token'];
console.log(access_token);
})
在我的 get 函数中:
$http({method: 'GET', url: 'api/posts/', headers: {'Authorization': 'Bearer '+self.access_token}}).then(function(response) {
self.phones = response.data;
});
我该如何做到这一点?
【问题讨论】:
标签: javascript angularjs oauth