Skip to content

Instantly share code, notes, and snippets.

@mitulbhalia
Forked from Rogichi/app.js
Last active August 29, 2015 14:12
Show Gist options
  • Save mitulbhalia/de8fdf2a9f2e4be8d5a6 to your computer and use it in GitHub Desktop.
Save mitulbhalia/de8fdf2a9f2e4be8d5a6 to your computer and use it in GitHub Desktop.
// How Publish a Tweet (Titanium)
// Full Codebird API is here: https://github.com/mynetx/codebird-js
// Codebird for Appcelerator Titanium. Using the Twitter API 1.1: https://gist.github.com/viezel/5781083
//THANKS VIEZEL
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');
///////////SET TWEET FUNCTION
function setTweet(){
cb.__call(
"statuses_update",
{"status": "Whohoo, I just tweeted!"},
function (reply) {
Ti.API.info("Respuesta al publicar: ");// ...
//Ti.API.info(reply);// ...
///////////INSPECT OBJECT
function inspeccionar(obj){
var msg = '';
for (var property in obj){
if (typeof obj[property] == 'function')
{
var inicio = obj[property].toString().indexOf('function');
var fin = obj[property].toString().indexOf(')')+1;
var propertyValue=obj[property].toString().substring(inicio,fin);
msg +=(typeof obj[property])+' '+property+' : '+propertyValue+' ;\n';
}
else if (typeof obj[property] == 'unknown')
{
msg += 'unknown '+property+' : unknown ;\n';
}
else
{
msg +=(typeof obj[property])+' '+property+' : '+obj[property]+' ;\n';
}
}
return msg;
}
//Ti.API.info(inspeccionar(reply));
//Ti.API.info(inspeccionar(reply.errors[0]));
//Ti.API.info(reply.httpstatus);
if(reply.httpstatus == 200)
alert("Tweet exitoso!!!");
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