简体   繁体   English

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

[英]Authentication using Apache Cordova for Visual Studio

I am working on a cross-platform Twitter app using Cordova in Visual Studio and I am stuck at the authentication of twitter account. 我正在使用Visual Studio中的Cordova开发一个跨平台的Twitter应用程序,我被困在twitter帐户的身份验证中。
When targeting Windows/Windows Phone, I can use Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync API and get the work done. 在定位Windows / Windows Phone时,我可以使用Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync API并完成工作。 But for Android or IOS I can't use the API as it is complaining that Windows is undefined. 但对于Android或IOS,我不能使用API​​,因为它抱怨Windows未定义。

Could someone help me with a sample code or suggestion on how should I be doing authentication using JavaScript ? 有人可以帮我提供一个示例代码或建议,我应该如何使用JavaScript进行身份验证?

I think you shouldn't rely on a Windows API on a cross-platform app, as it won't be available on any other platform than Windows. 我认为您不应该依赖跨平台应用程序上的Windows API,因为它不会在除Windows之外的任何其他平台上提供。 Instead you may want to authenticate with a javascript solution that works on every platform. 相反,您可能希望使用适用于每个平台的JavaScript解决方案进行身份验证。
There are several possibilities doing this in js, depending on what frameworks and libraries you're using in your app: You may authenticate using $.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 . 根据您在应用程序中使用的框架和库,有几种可能性在js中执行此操作:如果您使用jquery,则可以使用$ .ajax进行身份验证;如果使用角度,则可以使用$ http服务进行身份验证...如果您没有使用任何库,则可以使用XMLHttpRequest

I found a solution using InAppBrowser plugin from Cordova. 我找到了一个使用Cordova的InAppBrowser插件的解决方案。

Add the plugin to your project and then have the code below to authorize your app. 将插件添加到项目中,然后使用下面的代码来授权您的应用。

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.

相关问题 Apache Cordova Visual Studio 2015 iframe - Apache Cordova Visual Studio 2015 iframe Visual Studio 2015 Apache Cordova 构建错误 - Visual Studio 2015 Apache Cordova Build Error 在Visual Studio 2015中将Node.js代码与Apache Cordova应用程序一起使用 - Using Node.js code with an Apache Cordova Application in Visual Studio 2015 使用 Apache Cordova 时无法在 Visual Studio 2017 中构建应用程序 - Can't build app in Visual Studio 2017 while using Apache Cordova 无法在Visual Studio中使用Angular $ q库(Apache cordova) - Can't use Angular $q library in Visual Studio (Apache cordova) 安装适用于Visual Studio 2013的Apache Cordova的工具-CTP3 - installing tools for apache cordova for visual studio 2013 - CTP3 Apache Cordova Visual Studio 2015社区麦克风通信应用程序 - Apache Cordova Visual Studio 2015 Community microphone communication App 缺少带有Visual Studio 2015的Apache Cordova中的exec:SQLitePlugin.open - missing exec:SQLitePlugin.open in Apache Cordova with Visual Studio 2015 Visual Studio 2015 Apache Cordova Android应用程序Iframe - Visual Studio 2015 Apache Cordova Android App Iframe 使用Visual Studio检查Cordova中的Internet连接 - Check Internet Connectivity in cordova using visual studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM