[英]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.