Skip to content

Instantly share code, notes, and snippets.

@rcknr
Created July 27, 2013 18:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rcknr/6095720 to your computer and use it in GitHub Desktop.
Save rcknr/6095720 to your computer and use it in GitHub Desktop.
Update Twitter status with an image with a single request in Apps Script
function oAuthConfig() {
var oAuthConfig = UrlFetchApp.addOAuthService("twitter");
oAuthConfig.setAccessTokenUrl("http://api.twitter.com/oauth/access_token");
oAuthConfig.setRequestTokenUrl("http://api.twitter.com/oauth/request_token");
oAuthConfig.setAuthorizationUrl("http://api.twitter.com/oauth/authorize");
// Register an app at https://dev.twitter.com/apps/new to get the following key and secret
oAuthConfig.setConsumerKey("key");
oAuthConfig.setConsumerSecret("secret");
}
function postImage() {
oAuthConfig();
var boundary = "cuthere";
var picture = UrlFetchApp.fetch("https://twitter.com/images/resources/twitter-bird-white-on-blue.png").getBlob().setContentTypeFromExtension();
var status = "Test upload with media";
var requestBody = Utilities.newBlob("--"+boundary+"\r\n"+
"Content-Disposition: form-data; name=\"status\"\r\n\r\n"+status+"\r\n"+
"--"+boundary+"\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,
oAuthServiceName: "twitter",
oAuthUseToken: "always",
payload: requestBody
};
var request = UrlFetchApp.fetch("https://api.twitter.com/1.1/statuses/update_with_media.json", options);
}
@YiuriV
Copy link

YiuriV commented Dec 4, 2015

Hi, how could this code be adapted to work with the new twitter api 1.1?

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