Skip to content

Instantly share code, notes, and snippets.

@JigarM
Created July 3, 2014 12:25
Show Gist options
  • Save JigarM/8bae96f34dc04875549c to your computer and use it in GitHub Desktop.
Save JigarM/8bae96f34dc04875549c to your computer and use it in GitHub Desktop.
How Publish a Tweet (Titanium) using Codbird.js
var win = Ti.UI.createWindow();
// Main Window
var twitter = Ti.UI.createButton({
title : 'Set Tweet'
});
var accessToken = null;
var accessTokenSecret = null;
///////////LOAD ACCESS TOKEN
loadAccessToken = function(pService) {
Ti.API.info('Loading access token for service [' + pService + '].');
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, pService + '.config');
if (file.exists() == false) {
return;
}
var contents = file.read();
if (contents == null) {
return;
}
var config;
try {
config = JSON.parse(contents.text);
} catch(ex) {
return;
}
if (!config) {
return;
}
if (config.accessToken) {
accessToken = config.accessToken;
}
if (config.accessTokenSecret) {
accessTokenSecret = config.accessTokenSecret;
}
Ti.API.info('Loading access token: done [accessToken:' + accessToken + '][accessTokenSecret:' + accessTokenSecret + '].');
};
///////////SAVE ACCESS TOKEN
saveAccessToken = function(pService) {
Ti.API.info('Saving access token [' + pService + '].');
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, pService + '.config');
if (file == null) {
file = Ti.Filesystem.createFile(Ti.Filesystem.applicationDataDirectory, pService + '.config');
}
file.write(JSON.stringify({
accessToken : accessToken,
accessTokenSecret : accessTokenSecret
}));
Ti.API.info('Saving access token: done.');
};
///////////CLEAR ACCESS TOKEN
clearAccessToken = function(pService) {
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, pService + '.config');
if (file == null) {
file = Ti.Filesystem.createFile(Ti.Filesystem.applicationDataDirectory, pService + '.config');
}
file.write(JSON.stringify({
accessToken : null,
accessTokenSecret : null
}));
accessToken = null;
accessTokenSecret = null;
};
var Codebird = require("codebird");
var cb = new Codebird();
cb.setConsumerKey('ConsumerKey', 'Consumer Secret');
//cb.setConsumerKey('XXXXXXXXXXXXXXX', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
///////////SET TWEET FUNCTION
function setTweet() {
cb.__call("statuses_update", {
"status" : "Whohoo, I just tweeted!"
}, function(reply) {
Ti.API.info("Tweet Successful");
// ...
//Ti.API.info(reply);// ...
if (reply.httpstatus == 200)
alert("Tweet Successfully Posted!!!");
else
alert(reply.errors[0].message);
});
}
twitter.addEventListener('click', function(e) {
/////////// SET TWEET ///////////
//clearAccessToken('twitter');
loadAccessToken('twitter');
if (accessTokenSecret != null && accessToken != null) {
cb.setToken(accessToken, accessTokenSecret);
setTweet();
} else {
cb.__call("oauth_requestToken", {
oauth_callback : "oob"
}, function(reply) {
// stores it
cb.setToken(reply.oauth_token, reply.oauth_token_secret);
// gets the authorize screen URL
cb.__call("oauth_authorize", {}, function(auth_url) {
//window.codebird_auth = window.open(auth_url);
Ti.API.info(auth_url);
// ...
var window = Titanium.UI.createWebView({
height : "100%",
width : "100%",
url : auth_url
});
closeLabel = Ti.UI.createLabel({
textAlign : 'right',
font : {
fontWeight : 'bold',
fontSize : '12pt'
},
text : '(X)',
top : 0,
right : 0,
height : 14
});
window.add(closeLabel);
closeLabel.addEventListener('click', function(e) {
win.remove(window)
});
var destroyAuthorizeUI = function() {
Ti.API.info('destroyAuthorizeUI');
// remove the UI
try {
window.removeEventListener('load', authorizeUICallback);
win.remove(window);
window = null;
} catch(ex) {
Ti.API.info('Cannot destroy the authorize UI. Ignoring.');
}
};
var authorizeUICallback = function(e) {
Ti.API.info('authorizeUILoaded');
//alert('authorizeUILoaded');
//var val = window.evalJS('document.getElementById("PINFIELD").value');
var val = window.evalJS('window.document.querySelector(\'kbd[aria-labelledby="code-desc"] > code\').innerHTML');
Ti.API.info(val);
//alert(window.html);
if (val) {
destroyAuthorizeUI();
cb.__call("oauth_accessToken", {
oauth_verifier : val
}, function(reply) {
// store the authenticated token, which may be different from the request token (!)
cb.setToken(reply.oauth_token, reply.oauth_token_secret);
Ti.API.info(reply);
setTweet();
accessToken = reply.oauth_token;
accessTokenSecret = reply.oauth_token_secret;
saveAccessToken('twitter');
});
}
};
window.addEventListener('load', authorizeUICallback);
win.add(window);
});
});
}
});
win.add(twitter);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment