Authorization required each time I run my script to post a tweet in Google scripts


#1

Dear all, I am making a simple app in Google Spreadsheet that publish every 10 minutes some information scrapped from internet. The problem is that each time I run the macro, a new windows popups requesting me to allow to tweet. How to leave the authorization permanent?

My code:

var CONSUMER_KEY = "AAAAAA";
var CONSUMER_SECRET = "BBBBBBB";

function Twitting() {
    Logger.clear();
    var misdatos = SpreadsheetApp.getActiveSheet().getRange('B9').getValue();
    sendTweet(misdatos);
}

function getConsumerKey() {
  return CONSUMER_KEY;
}
 
function getConsumerSecret() {
  return CONSUMER_SECRET;
}

function sendTweet(mensaje) {
    oAuth();
    var tweet =  encodeString(mensaje);
    var enviar = "https://api.twitter.com/1.1/statuses/update.json?status=" + tweet; 
    var options =
    {
        "method": "POST",
        "oAuthServiceName":"twitter",
        "oAuthUseToken":"always"
    };

    try {
        var result = UrlFetchApp.fetch(enviar, options);
    } catch (e) {
        Logger.log(e.toString());
    }
}

function encodeString(q) {
    var str = encodeURIComponent(q);
    str = str.replace(/!/g,'%21');
    str = str.replace(/\*/g,'%2A');
    str = str.replace(/\(/g,'%28');
    str = str.replace(/\)/g,'%29');
    str = str.replace(/'/g,'%27');
    return str;
}

function oAuth() {
  var oauthConfig = UrlFetchApp.addOAuthService("twitter");
  
  oauthConfig.setAccessTokenUrl("https://api.twitter.com/oauth/access_token");
  oauthConfig.setRequestTokenUrl("https://api.twitter.com/oauth/request_token");
  oauthConfig.setAuthorizationUrl("https://api.twitter.com/oauth/authenticate");
  oauthConfig.setConsumerKey(getConsumerKey());
  oauthConfig.setConsumerSecret(getConsumerSecret());
  
  var requestData = {
    "method": "GET",
    "oAuthServiceName": "twitter",
    "oAuthUseToken": "always"
  };
  try {
    var result = UrlFetchApp.fetch("https://api.twitter.com/1/statuses/mentions.json",requestData);
  } catch(e) {
    Logger.log(e);
  }
  
  
}

#2

same problem here


#3

I have the same problem but I’m doing a GET. App is successfully added to approved apps, and I’ve revoked and re-added the app, as well as regenerating the ConsumerKey and ConsumerSecretKey, updating my code accordingly. Asks me to authorize every time, never returns data.