ferdhika31

Google Script Image Upload Twitter

May 9th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. function oAuthConfig() {
  2.  
  3. var oAuthConfig = UrlFetchApp.addOAuthService("twitter");
  4. oAuthConfig.setAccessTokenUrl("http://api.twitter.com/oauth/access_token");
  5. oAuthConfig.setRequestTokenUrl("http://api.twitter.com/oauth/request_token");
  6. oAuthConfig.setAuthorizationUrl("http://api.twitter.com/oauth/authorize");
  7. oAuthConfig.setConsumerKey("consumer");
  8. oAuthConfig.setConsumerSecret("secret");
  9.  
  10. }
  11. function postImage() {
  12. oAuthConfig();
  13. var boundary = "cuthere";
  14. var picture = UrlFetchApp.fetch("http://1cak.com/")
  15. .getBlob().setContentTypeFromExtension();
  16. var status = "Tweet text with image upload";
  17. var requestBody = Utilities.newBlob(
  18. "--"+boundary+"\r\n"
  19. + "Content-Disposition: form-data; name=\"status\"\r\n\r\n"
  20. + status+"\r\n"+"--"+boundary+"\r\n"
  21. + "Content-Disposition: form-data; name=\"media[]\"; filename=\""+picture.getName()+"\"\r\n"
  22. + "Content-Type: " + picture.getContentType()+"\r\n\r\n").getBytes();
  23.  
  24. requestBody = requestBody.concat(picture.getBytes());
  25. requestBody = requestBody.concat(Utilities.newBlob("\r\n--"+boundary+"--\r\n").getBytes());
  26.  
  27. var options = {
  28. method: "post",
  29. contentType: "multipart/form-data; boundary="+boundary,
  30. oAuthServiceName: "twitter",
  31. oAuthUseToken: "always",
  32. payload: requestBody
  33. };
  34. var request = UrlFetchApp.fetch("https://api.twitter.com/1.1/statuses/update_with_media.json", options);
  35. }
Add Comment
Please, Sign In to add comment