Skip to content

Instantly share code, notes, and snippets.

@zaxklone
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaxklone/abbe5524b6f465ed1245 to your computer and use it in GitHub Desktop.
Save zaxklone/abbe5524b6f465ed1245 to your computer and use it in GitHub Desktop.
Code snippets for Google Apps Script
Below are snippets that are meant
//
// Thank you +Martin Hawksey - you are awesome
function encodeString (q) {
// Update: 09/06/2013
// Google Apps Script is having issues storing oAuth tokens with the Twitter API 1.1 due to some encoding issues.
// Henc this workaround to remove all the problematic characters from the status message.
var str = q.replace(/\(/g,'{').replace(/\)/g,'}').replace(/\[/g,'{').replace(/\]/g,'}').replace(/\!/g, '|').replace(/\*/g, 'x').replace(/\'/g, '');
return encodeURIComponent(str);
}
/*oAuth implementation
Note this implements the new properties service
see the url below
https://developers.google.com/apps-script/guides/properties
Storing the Twitter credentials in the User Properties space
*/
function oAuth() {
var userProperties = PropertiesService.getUserProperties();
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/authorize");
oauthConfig.setConsumerKey(userProperties.getProperty("TWITTER_CONSUMER_KEY"));
oauthConfig.setConsumerSecret(userProperties.getProperty("TWITTER_CONSUMER_SECRET"));
}
function tweetFunction(text) {
var TWITTER_CONSUMER_KEY = "XXXXXXXXXXXXXXXXXXXXX";
var TWITTER_CONSUMER_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
var TWITTER_HANDLE = "accountname";
// DO NOT CHANGE ANYTHING BELOW THIS LINE
// Store variables
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty("TWITTER_CONSUMER_KEY", TWITTER_CONSUMER_KEY);
userProperties.setProperty("TWITTER_CONSUMER_SECRET", TWITTER_CONSUMER_SECRET);
userProperties.setProperty("TWITTER_HANDLE", TWITTER_HANDLE);
userProperties.setProperty("MAX_TWITTER_ID", 0);
oAuth();
var userProperties = PropertiesService.getUserProperties();
var twitter_handle = userProperties.getProperty("TWITTER_HANDLE");
var options =
{
"method": "POST",
"oAuthServiceName":"twitter",
"oAuthUseToken":"always"
};
var status = "https://api.twitter.com/1.1/statuses/update.json";
status = status + "?status=" + encodeString(""+text);
try {
var result = UrlFetchApp.fetch(status, options);
Logger.log(result.getContentText());
}
catch (e) {
GmailApp.sendEmail('zaxklone@gmail.com','shit failed yo',e.toString());
Logger.log(e.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment