oauth-io / oauth-phonegap

OAuth.io plugin for Apache Cordova/PhoneGap
195 stars 69 forks source link

Authentication!! #18

Closed donniemceduns closed 10 years ago

donniemceduns commented 10 years ago

Hello, do i have to do OAuth.popup() every time i want to post a status update to twitter?.Is there a method or function i can use to post status updates without having to authenticate each time i want to post an update?.Thanks

william26 commented 10 years ago

Hello, actually you just need to show a popup once, then save a reference to the result object that is returned in its callback. This object contains the access token, and several methods to perform requests (get, post, put, del, patch, me).

You could do something like this :

<!-- index.html -->
<!-- ... -->
<button id="post-button">Post Update</button>
<!-- ... -->
/* Your javascript file */
OAuth.initialize('your_public_key');

function post_update(twitter, update) {
    twitter.post('/1.1/statuses/update.json', {
        data: {
              status: update
        }
    })
         .done(function (response) {
               // handle response
         })
         .fail(function (error) {
              // handle error
         });
}
var twitter = undefined;
$('#post-button').on('touchstart', function () {
    if (twitter) {
         post_update(twitter, 'Hello World');
    } else {
         OAuth.popup('twitter', function (e, result) {
               if (e) {
                   console.log(e);
                   return;
               }
               twitter = result;
               post_update(twitter, 'Hello World');
         });
    }
});

Using the cache

Furthermore, as of 0.2.0 (which we will release quite soon), you'll be able to use the cache feature. This feature prevents your users from seeing a popup every time they launch your app. The access token can be put in cache for as long as it is valid. To use this feature, you will need to pass the cache option in the popup, like this :

OAuth.popup('twitter', {cache: true})
    .done(/* ... */)
    .fail(/* ... */);

If you want to check out the new features of 0.2.0, feel free to take a look at our develop branch.

I hope this helps :)

Edit (06-04-2014) : We released version 0.2.0 today.