Authentication using Apache Cordova for Visual Studio

后端 未结 2 551
时光说笑
时光说笑 2021-01-23 11:17

I am working on a cross-platform Twitter app using Cordova in Visual Studio and I am stuck at the authentication of twitter account.
When targeting Windows/Windows Phone, I

2条回答
  •  长发绾君心
    2021-01-23 12:10

    I found a solution using InAppBrowser plugin from Cordova.

    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;
           }
    }
    

提交回复
热议问题