secret
Last active

This is a really simple social app that demonstrates how to post to Twitter using Appcelerator Titanium.

  • Download Gist
app.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/**
* This is a really simple social app that demonstrates how to post to Twitter using Appcelerator Titanium.
* REQUIRED: DOWNLOAD the "social.js" file from http://appc.me/social.js
* and SAVE IT in your app's Resources directory.
*
* Get API access to Twitter here: https://dev.twitter.com/apps
* The consumer key and secret will be used later in this app. Make sure you check "Read & Write"!
*
* The latest version of this file can always be found at http://appc.me/social.sample.js
*
* Tweet me @dawsontoth with questions.
*/
 
/**
* We'll start by making a window with a button...
*/
var win = Ti.UI.createWindow({ backgroundColor: '#fff' });
var shareButton = Ti.UI.createButton({
width: 90, bottom: 10, height: 30,
title: 'Tweet "Hello, World!"'
});
win.add(shareButton);
win.open();
 
/**
* Now we include our JavaScript module. There's more info here on how this works: http://appc.me/jsmodules
*/
var social = require('social');
var twitter = social.create({
site: 'Twitter', // <-- this example is for Twitter. I'll expand this to other sites in the future.
consumerKey: '9U7332KAWsGzdIZDNpWkw', // <--- you'll want to replace this
consumerSecret: 'zG0SgCPfxFcwPyVatmYik9tUXMwcMQaKZjj2wOqQeag' // <--- and this with your own keys!
});
 
/**
* And when the user clicks on the button, share a message with the world!
* Note that this will show the authorization UI, if necessary.
*/
shareButton.addEventListener('click', function() {
twitter.share({
message: 'Hello, world!',
success: function() {
alert('Tweeted!');
},
error: function(error) {
alert('Oh no! ' + error);
}
});
});
 
/**
* Finally, here are some other methods you might want to use:
*/
/*
twitter.isAuthorized();
twitter.authorize(function() {
alert('Authorized!');
});
twitter.deauthorize();
*/

Hi Dawson

we try your code in iOS and it works fine...
same code in android dosen't works, we became allways the twitter screen with the message that the token is missing...

did you try it with android?

regards

Hey Dawson,
The code had been working great in my app. However, Twitter recently deprecated the REST API v1 and now I get an error message that I should migrate to API v1.1
Could you please point as to how I should correct this?

Thanks!

In social.js, look for this

var supportedSites = {
twitter: {
accessToken: 'https://api.twitter.com/oauth/access_token',
requestToken: 'https://api.twitter.com/oauth/request_token',
authorize: 'https://api.twitter.com/oauth/authorize?',
update: 'https://api.twitter.com/1/statuses/update.json'
}
};

change to

var supportedSites = {
twitter: {
accessToken: 'https://api.twitter.com/oauth/access_token',
requestToken: 'https://api.twitter.com/oauth/request_token',
authorize: 'https://api.twitter.com/oauth/authorize?',
update: 'https://api.twitter.com/1.1/statuses/update.json'
}
};

Also, make sure your app in twitter has read and write permissions. Regenerate token if needed.

I made these changes but still can't get v1.1. working:

//1.1 instead of 1
update: 'https://api.twitter.com/1.1/statuses/update.json'

//in "send"
message.parameters.push(['oauth_version', "1.0"]);
client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

The token is retrieved, but a share post is not validated (error 32).

Can anyone confirm they got this script working for 1.1?

+1
can't get this to work... any suggestions?

var supportedSites = {
    twitter: {
        accessToken: 'https://api.twitter.com/oauth/access_token',
        requestToken: 'https://api.twitter.com/oauth/request_token',
        authorize: 'https://api.twitter.com/oauth/authorize?',
        update: 'https://api.twitter.com/1.1/statuses/update.json'
    }
};

should make it work for api 1.1 for posting

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.