401未经授权的错误读取Azure移动服务表作为认证的用户(Javascript)

401 Unauthorized Error Reading Azure Mobile Services Table as an Authenticated User (Javascript)

本文关键字:认证 用户 Javascript 移动 授权 错误 读取 Azure 服务      更新时间:2023-09-26

我有一个Azure Mobile Services表,其中读权限设置为"Only Authenticated Users"。

我遵循下面的示例https://azure.microsoft.com/en-us/documentation/articles/mobile-services-html-get-started-users/,并使用以下代码对用户进行身份验证:

    function logIn() {
    client.login("microsoftaccount").done(function (results) {
        console.log(results);
        //save auth data to local storage
        sessionStorage.loggedInUser = JSON.stringify(client.currentUser);
        //route to the next page based on the type of user that is logged  in
        getUserByUserID();
    }, function (err) {
        alert("Error: " + err);
    });
}

存储客户端。用户通过身份验证后,本地机器上sessionStorage中的currentUser信息。然后导航到另一个网页,在那里创建MobileServiceClient的新实例,并从前面提到的sessionStorage更新客户端变量的currentUser变量。我使用下面的代码:

//load the session data into the client object
if (sessionStorage.loggedInUser) {
    client.currentUser = JSON.parse(sessionStorage.loggedInUser);
    console.log("getting stored user: " + JSON.parse(sessionStorage.loggedInUser));
}

然而,当尝试使用下面的代码从表中读取时,我得到一个"401:未经授权的错误":

var query = client.getTable("towingProfiles").read().done(function (results) {
    console.log(JSON.stringify(results));
    _.each(results, function(towingProfile){
        towingProfiles.add(towingProfile);
    })
}, function (err) {
    alert("Error: " + err);
});

我不知道为什么这个不工作,并感谢您的帮助。

问题是我在制作MobileServiceClient时没有设置正确的应用程序密钥