Visual Studio 使用 Apache Cordova 进行身份验证

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 进行身份验证的建议吗?

我认为您不应该依赖 cross-platform 应用程序上的 Windows API,因为它在 Windows 以外的任何其他平台上都不可用.相反,您可能希望使用适用于每个平台的 javascript 解决方案进行身份验证。
根据您在应用程序中使用的框架和库,在 js 中有多种可能性:您可以使用 $.ajax if you're using jquery or the $http service if you're using angular... If you're not using any library you could use a 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;
       }
}