【问题标题】:Authentication using Apache Cordova for Visual Studio使用 Apache Cordova for Visual Studio 进行身份验证
【发布时间】:2016-01-14 04:01:26
【问题描述】:
我正在使用 Visual Studio 中的 Cordova 开发跨平台 Twitter 应用程序,但我被困在 Twitter 帐户的身份验证中。
当面向 Windows/Windows Phone 时,我可以使用 Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync API 并完成工作。但对于 Android 或 IOS,我无法使用 API,因为它抱怨 Windows 未定义。
有人可以帮助我提供有关如何使用 JavaScript 进行身份验证的示例代码或建议吗?
【问题讨论】:
标签:
javascript
android
ios
visual-studio-2015
visual-studio-cordova
【解决方案1】:
我认为您不应该在跨平台应用程序上依赖 Windows API,因为它在 Windows 以外的任何其他平台上均不可用。相反,您可能希望使用适用于每个平台的 javascript 解决方案进行身份验证。
根据您在应用程序中使用的框架和库,在 js 中执行此操作有多种可能性:如果您使用 jquery,则可以使用 $.ajax 进行身份验证;如果您使用 angular,则可以使用 $http 服务...如果您不使用任何库,则可以使用XMLHttpRequest。
【解决方案2】:
我找到了使用 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;
}
}