Twitter Authentication Using hello.js and Cordova

2.7k Views Asked by At

I modified the Cordova example provided on the hello.js homepage (https://github.com/MrSwitch/hellojs-phonegap-demo) to login using twitter but I cannot make it work. Basically, when I try the original code provided in the twitter hello.js example it works fine and it connects to the twitter page for the usual permissions! when I tried my modified code it opens up the localhost page which is very weird

The code I use is pretty standard and a simple modification of the original one

This is the code I use:

        // Initate the library
        hello.init({
            google : ' ... ',
            facebook : ' ... ',
            twitter : 'mkjOc4ESXs5ZVFan5l0hTfSFc',
            windows : ' ... ',
        }, {
            //
            // Define the OAuth2 return URL
            // This can be anything you like, providing its the callback which you have registered with the providers for OAuth2
            // It could even be localhost, e.g. http://localhost/somepath as phonegap is not run from a domain so SameOrigin breaks, instead we take advantage of being able to read the popups URL in PhoneGap
            // redirect_uri : 'http://adodson.com/hello.js/redirect.html'
            redirect_uri : 'http://localhost'
        });

and this is the button

    <button onclick="hello('twitter').login(loginHandler)">Twitter</button>
1

There are 1 best solutions below

0
On

If you get a login error like

function loginHandler(resp){
   if(resp.error){
       // Error object type 'invalid_credentials'
   }
}

Then then this is likely because the client id is not recognised by the Auth Proxy. OAuth1 services like twitter need a authentication proxy service because it relies on the secret which can't be exposed in the web client.

You can use the recommended service at https://auth-server.herokuapp.com/ which HelloJS is set up to use by default, or run your own, see http://adodson.com/hello.js/#oauth-proxy for more info.