繁体   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