Last active
November 19, 2021 12:51
-
-
Save laurenancona/1f3b8ce7a50e7d24ad04adccebce3fa3 to your computer and use it in GitHub Desktop.
Add custom functions to a Google sheet to pull user info directly from Twitter REST API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on script by @SarahMarshall here: | |
// http://sarahmarshall.io/post/70812214349/how-to-add-twitter-follower-counts-to-a-google | |
var id = '@username'; // Replace with your username, then delete this line after running script 1st time | |
var CONSUMER_KEY = 'INSERT CONSUMER KEY'; // Create an application at https://dev.twitter.com | |
var CONSUMER_SECRET = 'INSERT CONSUMER SECRET'; // Create an application at https://dev.twitter.com | |
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); | |
} | |
//Get count of tweets by user | |
function getTwitterUserTweetCount(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.statuses_count | |
} | |
Logger.log(result); | |
} | |
//Get verified status | |
function getTwitterUserVerified(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.verified | |
} | |
Logger.log(result); | |
} | |
//Get user profile image | |
function getTwitterUserProfileImage(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.profile_image_url_https | |
} | |
Logger.log(result); | |
} | |
// Get count of tweets for a user since a date | |
// https://api.twitter.com/1.1/search/tweets.json?q=from%3Aphiladelphiagov&src=typd+since:2016-10-1 | |
function getTwitterUserTweetCount(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/search/tweets.json?q=from%3Aphiladelphiagov&src=typd+since:2016-10-1' | |
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.profile_image_url_https | |
} | |
Logger.log(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adds 4 custom functions to a Google sheet where
id
= a Twitter handle in format:@Twitter
getTwitterUserFollowers(id)
Returns # of followers for the givenid
getTwitterUserTweetCount(id)
Returns total # of tweets for the givenid
getTwitterUserProfileImage(id)
Returns url of profile image for the givenid
getTwitterUserVerified(id)
Returns verified status (boolean) for the givenid
Example gSheet