skip to Main Content

I have gathered some functions and bundled them into a single Google Scripts file that I use with Google Sheets. The functions are: (1) getTwitterUserFollowers(id), (2) getTwitterUserFollowing(id), (3) getTwitterUserName(id) and the (4) postBlockTwitterUser(id).

While the first 3 functions work perfectly, I can’t get the 4th to work (the postBlockTwitterUser(id). Keep on getting:

"Request failed for https://api.twitter.com/1.1/blocks/create.json?screen_name=EladRatson returned code 403. Truncated server response: {"errors":[{"code":220,"message":"Your credentials do not allow access to this resource."

What am i doing wrong?

var CONSUMER_KEY = 'M2VyQoC2571PyNAdHmOnGYxMj';
var CONSUMER_SECRET = '8yNJGrp2W0fDRQvXgbe8zOgQWC6lQOjrxhI7M6iogLbHyanor0';



function getTwitterUserFollowers(id) {

// Encode consumer key and secret
  var tokenUrl = "https://api.twitter.com/oauth2/token";
  var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

//  Obtain a bearer token with HTTP POST request
  var tokenOptions = {
    headers : {
      Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
    },
  method: "post",
  payload: "grant_type=client_credentials"
};

  var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
  var parsedToken = JSON.parse(responseToken);
  var token = parsedToken.access_token;

// Authenticate Twitter API requests with the bearer token
  var apiUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='+id;
  var apiOptions = {
    headers : {
      Authorization: 'Bearer ' + token
    },
  "method" : "get"
  };

  var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);
  var result = "";

  if (responseApi.getResponseCode() == 200) {

    // Parse the JSON encoded Twitter API response
    var tweets = JSON.parse(responseApi.getContentText());
    return tweets.followers_count
  }

  Logger.log(result);

}



function getTwitterUserFollowing(id) {

 // Encode consumer key and secret
 var tokenUrl = "https://api.twitter.com/oauth2/token";
 var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

 //  Obtain a bearer token with HTTP POST request
 var tokenOptions = {
   headers : {
     Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
   },
   method: "post",
   payload: "grant_type=client_credentials"
 };

 var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
 var parsedToken = JSON.parse(responseToken);
 var token = parsedToken.access_token;

 // Authenticate Twitter API requests with the bearer token
 var apiUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='+id;
 var apiOptions = {
   headers : {
     Authorization: 'Bearer ' + token
   },
   "method" : "get"
 };

 var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

 var result = "";

 if (responseApi.getResponseCode() == 200) {

   // Parse the JSON encoded Twitter API response
   var tweets = JSON.parse(responseApi.getContentText());
   return tweets.friends_count

 }

 Logger.log(result);

}



function getTwitterUserName(id) {

 // Encode consumer key and secret
 var tokenUrl = "https://api.twitter.com/oauth2/token";
 var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

 //  Obtain a bearer token with HTTP POST request
 var tokenOptions = {
   headers : {
     Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
   },
   method: "post",
   payload: "grant_type=client_credentials"
 };

 var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
 var parsedToken = JSON.parse(responseToken);
 var token = parsedToken.access_token;

 // Authenticate Twitter API requests with the bearer token
 var apiUrl = 'https://api.twitter.com/1.1/users/show.json?screen_name='+id;
 var apiOptions = {
   headers : {
     Authorization: 'Bearer ' + token
   },
   "method" : "get"
 };

 var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

 var result = "";

 if (responseApi.getResponseCode() == 200) {

   // Parse the JSON encoded Twitter API response
   var tweets = JSON.parse(responseApi.getContentText());
   return tweets.name

 }

 Logger.log(result);

}




function postBlockTwitterUser(id) {

 // Encode consumer key and secret
 var tokenUrl = "https://api.twitter.com/oauth2/token";
 var tokenCredential = Utilities.base64EncodeWebSafe(
   CONSUMER_KEY + ":" + CONSUMER_SECRET);

 //  Obtain a bearer token with HTTP POST request
 var tokenOptions = {
   headers : {
     Authorization: "Basic " + tokenCredential,
     "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" 
   },
   method: "post",
   payload: "grant_type=client_credentials"
 };

 var responseToken = UrlFetchApp.fetch(tokenUrl, tokenOptions);
 var parsedToken = JSON.parse(responseToken);
 var token = parsedToken.access_token;

 // Authenticate Twitter API requests with the bearer token
 var apiUrl = 'https://api.twitter.com/1.1/blocks/create.json?screen_name='+id;
 var apiOptions = {
   headers : {
     Authorization: 'Bearer ' + token
   },
   "method" : "post"
 };

 var responseApi = UrlFetchApp.fetch(apiUrl, apiOptions);

 var result = "";

 if (responseApi.getResponseCode() == 200) {

   // Parse the JSON encoded Twitter API response
   var tweets = JSON.parse(responseApi.getContentText());
   return tweets.name

 }

 Logger.log(result);

}

2

Answers


  1. You’re trying to use app-only authentication OAuth, but you need to use 3-legged authentication with a user token in order to use the blocks endpoint. You’ll need to modify your code to include all four tokens (consumer key and secret and user token and secret) if you want to post to that endpoint.

    Login or Signup to reply.
  2. @andy-piper can you please modify the above code for 3 legged authentication?

    I am also using the above script, but don’t know how to post tweets using it. I have the access token and secret but don’t know how to use it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search