Recently, I’ve been creating an app that rewards users for tweeting messages. However, I wanted to avoid using OAuth authentication and full integration of the Twitter API but still be able to reward users for tweeting. Luckily, I found a way of doing so using the Tweet button and something called Web Intents.
The Twitter JavaScript API needs to be used (which is simple to integrate by copy-and-pasting the plugin code into your page). The functionality works as follows:
- User presses tweet button
- User logs into twitter (in popup) if they are not already logged in
- User previews message and confirms the tweet (in popup)
- Tweet is posted and popup closes
- JavaScript callback is triggered
The Tweet button handles steps 2-5 above so the only points of integration required are adding the tweet button and the callback implementation.
I integrated the Tweet button as follows:
Then, I added some extra JavaScript to the page to listen for the callback and trigger the function to reward the user:
Once a tweet has been posted, twitter will trigger the tweet
event. The web intent we created will listen for the tweet
event and then trigger the reward_user
function.
Simples!
UPDATE (28th April 2014):
The code has been updated after Ryan spotted that the previous code was no longer working. You can see the working version of the new code on JSFiddle.