1

我正在使用Visual Studio中的Cordova進行跨平臺的Twitter應用程序工作,並且我被卡在Twitter帳戶的身份驗證中。
針對Windows/Windows Phone時,我可以使用Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync API並完成工作。但對於Android或IOS我無法使用該API,因爲它抱怨Windows未定義。使用Apache Cordova for Visual Studio進行身份驗證

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

回答

1

我發現了一個使用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; 
     } 
} 
2

我認爲您不應該依賴跨平臺應用程序上的Windows API,因爲它不會在Windows以外的任何其他平臺上使用。相反,您可能想要使用適用於每個平臺的JavaScript解決方案進行身份驗證。
在js中這樣做有幾種可能性,具體取決於您在應用中使用的框架和庫:如果您正在使用jquery或$ http服務(如果使用角度),則可以使用$.ajax進行身份驗證...如果你沒有使用任何庫,你可以使用XMLHttpRequest

+0

我會盡力回覆。 – Jagannath

+0

我剛剛發佈了適用於我的解決方案。 – Jagannath