2016-12-24 21 views
0

私のWinJSアプリケーションをしばらく休止状態にしてから戻ってきて、ボタンをクリックすると何らかの理由で私のバックエンドへの呼び出しが機能しません。MobileServices.web.js不正なAPI呼び出し

サーバーから「Unauthorized」エラーが発生します。

ユーザーまたは何かを再認証するように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); 
}); 
+0

は、Windows/Windowsの携帯電話アプリケーションを作成するためにWinJSを使用していますか?モバイルアプリをどのように認証しますか?問題をよりよく理解するために、より多くの情報を提供してください。 –

+0

私は自分の答えを更新しました。私が経験している問題は、いつリフレッシュトークンルーチンを呼び出すかということです。失敗する点は、クライアントオブジェクトを使用してサーバーにapi呼び出しを行うときです。例えばclient.invokeApi、テーブル呼び出し、またはclient._request呼び出し。これらの呼び出しは、認証トークンが期限切れになると失敗します。その前にリフレッシュを呼び出す必要があります。ユーザーマシンがアイドル状態になって復帰した場合にトークンを更新できるように、どうすれば設定できますか? –

+0

リフレッシュトークンの使い方について[このブログの投稿](https://shellmonger.com/2016/04/13/30-days-of-zumo-v2-azure-mobile-apps-day)を参照することをお勧めします-7-リフレッシュ・トークン/)。 –

答えて

1

の代わりに、私はちょうど彼らがアプリに戻すだけでなく、トークンに彼らがアイドル状態になっているすべての59秒を更新したときにユーザートークンを更新し、クライアントのアイドル・ルーチンを組み込んだ、すべてのAPI呼び出しの周りの約束を包みます。

すべての激しい目的のために、彼らは常に有効なトークンまたは永久状態を持っています。

$(document).idle({ 
    onIdle: function() { 
     // refresh user token 
     if (User.Person !== null) 
      User.Person.reauthenticate().done(); 
    }, 
    onActive: function() { 
     // when the user returns refresh their token 1 more time 
     if (User.Person !== null) 
      User.Person.reauthenticate().done(); 
    }, 
    idle: 59000, // 59 seconds 
    recurIdleCall: true // will keep refreshing every 59 seconds 
}); 

https://github.com/kidh0/jquery.idle

関連する問題