Missing } after function body - But it's there!


#1

Hi everyone. New to this, but trying to build a script that pulls Twitter follower numbers into a google document. Here is the code I am using. I get an error message when I try to run it that says Missing } after function body. (line 60, file "Code")

My script is this:

/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/
function getUserFollowers(id) {

//Replace the line below with your Twitter handle

var id = "@worldvisionjobs";

var CONSUMER_KEY = ''; // Register your app with Twitter.
 var CONSUMER_SECRET = ''; // Register your app with Twitter.

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);

}

Thanks for your help.


#2

Please edit / remove & reset your consumer_key and consumer_secret - you need to keep those private. To reset keys, go to https://apps.twitter.com/ find the app, and hit “Regenerate Consumer Key and Secret” under the “Keys & Access Tokens” Tab

I’m not very good with javascript but i’m pretty sure if you delete:
function getTwitterUserFollowers(id) {
line and call getUserFollowers(id) it should work.

It’s complaining about a missing } because function getUserFollowers(id) { is never closed. The last } is closing function getTwitterUserFollowers(id) {

Check out https://tags.hawksey.info/ for a good implementation of Twitter API stuff that gets stored into Google Docs.


#3

Thanks for your help. Oopsies. I deleted and reset the Key. I updated the code and now I am g
getting another error. New Code:

/**
* Retrieves all the rows in the active spreadsheet that contain data and logs the
* values for each row.
* For more information on using the Spreadsheet API, see
* https://developers.google.com/apps-script/service_spreadsheet
*/


var CONSUMER_KEY = ''; // Register your app with Twitter.
 var CONSUMER_SECRET = ''; // Register your app with Twitter.

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);

}

But now I am getting this error when I try to play:
Request failed for https://api.twitter.com/oauth2/token returned code 403. Truncated server response: {"errors":[{"code":99,"message":"Unable to verify your credentials","label":"authenticity_token_error"}]} (use muteHttpExceptions option to examine full response) (line 31, file "Code") any ideas?


#4

It seems that either your tokens are wrong or you do not specify them correctly. I am not very familiar with App-Only (Bearer) token authentication so I will have to check the docs again how to do this.