Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Created October 8, 2011 19:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronksaunders/1272760 to your computer and use it in GitHub Desktop.
Save aaronksaunders/1272760 to your computer and use it in GitHub Desktop.
Source code to upload an image using twitters new photo api
var sendTwitterImage = function(postParams, pSuccessCallback, pErrorCallback) {
var finalUrl = '';
// authorize user if not authorized, and call this in the callback
if(!authorized && ( typeof (auth) == 'undefined' || auth === true)) {
authorize(function(retval) {
if(!retval) {
// execute the callback function
if( typeof (callback) == 'function') {
callback(false);
}
return false;
} else {
sendTwitterImage(postParams, pSuccessCallback, pErrorCallback);
}
});
}
// user is authorized so execute API
else {
var url = "http://upload.twitter.com/1/statuses/update_with_media.json";
// VARIABLES
var initparams = params;
if(params != null) {
params = params + "&";
}
var message = set_message(url, "POST");
message.parameters.push(['oauth_token', cfg.access_token]);
OAuth.SignatureMethod.sign(message, accessor);
var XHR = Ti.Network.createHTTPClient();
XHR.open("POST", url);
// on success, grab the request token
XHR.onload = function() {
Ti.API.debug("XHR.onload "+XHR.responseText);
};
// on error, show message
XHR.onerror = function(e) {
Ti.API.debug("XHR.onerror "+e);
}
// if we are getting request tokens do not set the HTML header
if( typeof (setHeader) == 'undefined' || setHeader == true) {
var init = true;
var header = "OAuth ";
for(var i = 0; i < message.parameters.length; i++) {
if(init) {
init = false;
} else {
header = header + ",";
}
header = header + message.parameters[i][0] + '="' + escape(message.parameters[i][1]) + '"';
}
header = OAuth.getAuthorizationHeader("", message.parameters);
XHR.setRequestHeader("Authorization", header);
if (Ti.Platform.osname === 'iphone') {
XHR.setRequestHeader("Content-Type", "multipart/form-data");
}
}
XHR.send(postParams);
}
};
this.sendTwitterImage = sendTwitterImage;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment