var CONSUMER_KEY = '...'; var CONSUMER_SECRET = '...'; /** * Authorizes and makes a request to the Twitter API. */ function run() { var service = getService_(); if (service.hasAccess()) { var url = 'https://api.twitter.com/1.1/statuses/update.json'; var payload = { status: 'It\'s a tweet!' }; var response = service.fetch(url, { method: 'post', payload: payload }); var result = JSON.parse(response.getContentText()); Logger.log(JSON.stringify(result, null, 2)); } else { var authorizationUrl = service.authorize(); Logger.log('Open the following URL and re-run the script: %s', authorizationUrl); } } /** * Reset the authorization state, so that it can be re-tested. */ function reset() { var service = getService_(); service.reset(); } /** * Configures the service. */ function getService_() { return OAuth1.createService('Twitter') // Set the endpoint URLs. .setAccessTokenUrl('https://api.twitter.com/oauth/access_token') .setRequestTokenUrl('https://api.twitter.com/oauth/request_token') .setAuthorizationUrl('https://api.twitter.com/oauth/authorize') // Set the consumer key and secret. .setConsumerKey(CONSUMER_KEY) .setConsumerSecret(CONSUMER_SECRET) // Set the name of the callback function in the script referenced // above that should be invoked to complete the OAuth flow. .setCallbackFunction('authCallback') // Using a cache will reduce the need to read from // the property store and may increase performance. .setCache(CacheService.getUserCache()) // Set the property store where authorized tokens should be persisted. .setPropertyStore(PropertiesService.getUserProperties()); } /** * Handles the OAuth callback. */ function authCallback(request) { var service = getService_(); var authorized = service.handleCallback(request); if (authorized) { return HtmlService.createHtmlOutput('Success!'); } else { return HtmlService.createHtmlOutput('Denied'); } }