Skip to content

Instantly share code, notes, and snippets.

@dawsontoth
Created June 3, 2011 22:21
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save dawsontoth/2eabc31db388144b3abc to your computer and use it in GitHub Desktop.
Save dawsontoth/2eabc31db388144b3abc to your computer and use it in GitHub Desktop.
This is a really simple social app that demonstrates how to post to Twitter using Appcelerator Titanium.
/**
* 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();
*/
@ucola
Copy link

ucola commented Oct 9, 2012

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

@sernac
Copy link

sernac commented Jun 13, 2013

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!

@sergiot
Copy link

sergiot commented Jun 27, 2013

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.

@mattjgarland
Copy link

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?

@flobby
Copy link

flobby commented Jul 18, 2013

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

@adin234
Copy link

adin234 commented Feb 18, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment