簡體   English   中英

使用Apache Cordova for Visual Studio進行身份驗證

[英]Authentication using Apache Cordova for Visual Studio

我正在使用Visual Studio中的Cordova開發一個跨平台的Twitter應用程序,我被困在twitter帳戶的身份驗證中。
在定位Windows / Windows Phone時,我可以使用Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync API並完成工作。 但對於Android或IOS,我不能使用API​​,因為它抱怨Windows未定義。

有人可以幫我提供一個示例代碼或建議,我應該如何使用JavaScript進行身份驗證?

我認為您不應該依賴跨平台應用程序上的Windows API,因為它不會在除Windows之外的任何其他平台上提供。 相反,您可能希望使用適用於每個平台的JavaScript解決方案進行身份驗證。
根據您在應用程序中使用的框架和庫,有幾種可能性在js中執行此操作:如果您使用jquery,則可以使用$ .ajax進行身份驗證;如果使用角度,則可以使用$ http服務進行身份驗證...如果您沒有使用任何庫,則可以使用XMLHttpRequest

我找到了一個使用Cordova的InAppBrowser插件的解決方案。

將插件添加到項目中,然后使用下面的代碼來授權您的應用。

var self = this;
var ref = cordova.InAppBrowser.open(startURI, '_blank', 'location=yes');
ref.show();    
ref.addEventListener('loadstart', function (event) {
   if (event.url && event.url.match('oauth_verifier'))
   {                            
       ref.close();
       self._continueAuthentication(event.url, callback);
   } 
});

ref.addEventListener('loadstop', function (event) {
});

ref.addEventListener('exit', function (response) {
});

_continueAuthentication: function (returnedTokens, callback) {
        var self = this, oauthVerifier, oauthToken;
        var responseKeyValPairs = returnedTokens.split("?")[1].split("&");

        //Disect the important parts
        for (i = 0; i < responseKeyValPairs.length; i++) {
            splits = responseKeyValPairs[i].split("=");
            switch (splits[0]) {
                case "oauth_verifier":
                    oauthVerifier = splits[1];
                    break;
                case "oauth_token":
                    oauthToken = splits[1];
                    break;
       }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM