下面的代码如何获取Oauth token?
var mobileServiceClient = new WindowsAzure.MobileServices.MobileServiceClient('https://...azurewebsites.net');
mobileService.login("facebook", $scope.token).done(function (results) {
$scope.token = result.mobileServiceAuthenticationToken;
//??? how to get the first/renewed token?
}
再次打开登录页面时出现以下错误。
Error: 'token' is expected to be a value of type object, not string. at Object.c.createError (azure-mobile-apps-client.min.js:17) at azure-mobile-apps-client.min.js:17 at new b (azure-mobile-apps-client.min.js:17) at d.login (azure-mobile-apps-client.min.js:17) at ChildScope.$scope.signWithFackBook (controllers.js:10) at fn (eval at compile (ionic.bundle.js:27643), :4:239) at ionic.bundle.js:65429 at ChildScope.$eval (ionic.bundle.js:30400) at ChildScope.$apply (ionic.bundle.js:30500) at HTMLButtonElement. (ionic.bundle.js:65428)
BTW, I want the page to redirect to the main page if the token is valid.
.controller('LoginCtrl', function ($scope, $rootScope, $state) {
if ($scope.token ....is good...) // Need to test if the token is good
{
$state.go('tab.events');
}
$scope.signWithFackBook = function () {
$rootScope.client.login('facebook', $scope.token).done(function (result) {
$scope.token = result.mobileServiceAuthenticationToken;
$state.go('tab.events');
}, function (error) {
console.error(error);
alert('Failed to login!');
});
}
})
最佳答案
基于the API documentation ,login()
函数中的可选参数 token
需要作为特定对象提供,而不是带有现有 OAuth token 的字符串。这就提出了你的问题。
Error: 'token' is expected to be a value of type object, not string.
实际上,您可以使用以下内容:$rootScope.client.currentUser.mobileServiceAuthenticationToken
来获取经过身份验证的 token 。
.controller('LoginCtrl', function ($scope, $rootScope, $state) {
if ($rootScope.client.currentUser && $rootScope.client.currentUser.mobileServiceAuthenticationToken) {
$state.go('tab.events');
}
$scope.signWithFackBook = function () {
$rootScope.client.login('facebook').done(function (result) {
$state.go('tab.events');
}, function (error) {
console.error(error);
alert('Failed to login!');
});
}
})
关于javascript - 如何获取token并保存以供以后使用而不需要每次登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524293/