Skip to content

Instantly share code, notes, and snippets.

Created June 21, 2018 15:01
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save msadig/30dccada8104adc823336eaadce1cc6c to your computer and use it in GitHub Desktop.
Post media files to Twitter using Google Apps Script
function getTwitterService() {
// Check
// for the docs
return OAuth1.createService('twitter')
// Set the endpoint URLs.
// Set the consumer key and secret.
// Set the name of the callback function in the script referenced
// above that should be invoked to complete the OAuth flow.
// Set the property store where authorized tokens should be persisted.
function tweetPicFromURL(url) {
try {
var boundary = "cuthere";
var picture = UrlFetchApp.fetch(url).getBlob().setContentTypeFromExtension();
var status = "Test status";
var requestBody = Utilities.newBlob("--"+boundary+"\r\n"+
"Content-Disposition: form-data; name=\"status\"\r\n\r\n"+status+"\r\n"+
"Content-Disposition: form-data; name=\"media[]\"; filename=\""+picture.getName()+"\"\r\n"+
"Content-Type: "+picture.getContentType()+"\r\n\r\n").getBytes();
requestBody = requestBody.concat(picture.getBytes());
requestBody = requestBody.concat(Utilities.newBlob("\r\n--"+boundary+"--\r\n").getBytes());
var options =
method: "post",
contentType: "multipart/form-data; boundary="+boundary,
payload: requestBody
// twitter stuff
var api = '';
var twitterService = getTwitterService();
var response = twitterService.fetch(api, options);
return response;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment