Solved: OAuth Error when posting a tweet with image in JavaScript code



I’m using this code in a Google Script file:

function oAuthConfig() {
  var oAuthConfig = UrlFetchApp.addOAuthService("twitter");
function postImage() {
  var boundary = "cuthere";
  var picture = UrlFetchApp.fetch("")
  var status = "Tweet text with image upload";
  var requestBody = Utilities.newBlob(
    + "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("", options);

But I keep getting “OAuth Error”.

Thanks in advance for your help.


While I couldn’t figure what the difference is, I used a modified version of the code that can be found here and it works flawlessly.


That’s great, glad it is working now and thanks for sharing the alternative code here, that will be really helpful to other developers! :thumbsup:


I am using above code, but i am getting the following error -
ReferenceError: UrlFetchApp is not defined(…)


You’re doing this in Google Apps Script, right? That’s what this example is for.