0
當我離開我的WinJS應用程序休眠一段時間然後再回到它,並且我點擊一個按鈕,出於某種原因,我對後端的調用不起作用。MobileServices.web.js未授權的API調用
我從服務器收到「未經授權」錯誤。
如何修改invokeApi以便重新驗證用戶或其他東西?
有沒有人有使用mobileservices.web.js的經驗,以及如何讓最終用戶永久登錄而不必重新進行身份驗證?
謝謝。
client.invokeApi("getTopForumsTotal", {
method: "post"
}).then(function (results) {
// do something
}, function (error) {
WinJS.log(error);
});
我使用winjs mobileService來認證用戶。
client.login("microsoftaccount").done(function (results) {
// Create a credential for the returned user.
credential = new Windows.Security.Credentials.PasswordCredential("myapp", results.userId, results.mobileServiceAuthenticationToken);
vault.add(credential);
completeDispatcher();
}, function (error) {
WinJS.log(JSON.stringify(error));
errorDispatcher(error);
});
這就是我用來刷新最終用戶的令牌。
client._request("GET", "/.auth/refresh", null, null, {
accept: "application/json",
"ZUMO-API-VERSION": "2.0.0"
}, [], (error, response) => {
if (!error) {
var userObject = JSON.parse(response.responseText)
if (userObject.authenticationToken) {
client.currentUser.mobileServiceAuthenticationToken = userObject.authenticationToken;
testCall().done(function (success) {
if (success) {
credential = new Windows.Security.Credentials.PasswordCredential("myapp", userObject.user.userId, userObject.authenticationToken);
vault.add(credential);
authenticated = true;
completeDispatcher();
}
else errorDispatcher('testCall API does not exist');
});
}
else errorDispatcher('no authentication token returned');
}
else errorDispatcher(error);
});
你使用WinJS創建Windows/Windows Phone應用程序?你如何驗證你的手機應用程序?請提供更多信息以幫助我們更好地理解您的問題。 –
我已經更新了我的答案。我遇到的問題是何時調用刷新令牌例程。失敗的要點是使用客戶端對象對服務器進行api調用時。例如client.invokeApi,表格調用或client._request調用。如果身份驗證令牌過期,則這些調用將失敗,因此必須在此之前調用刷新。如何設置它以便在用戶機器閒置並返回時可以刷新令牌? –
關於如何使用刷新標記我建議您參考[本博文](https://shellmonger.com/2016/04/13/30-days-of-zumo-v2-azure-mobile-apps-day -7-刷新的令牌/)。 –