aa5ゴミ箱: GoogleAppsScriptでTwitterに投稿する方法
こちらのサイトを参考に事前準備をすませる。
kijtra.com
こちらの方が画像がありわかりやすい。
GAS(Google Apps Script)でお手軽にbotを作る方法。 - usakoyama
Twitterの登録はこちらがわかりやすい。
ここからコピペ
var CONSUMER_KEY = '...'; //入力 var CONSUMER_SECRET = '...'; //入力 /** * Authorizes and makes a request to the Twitter API. */ function run(text) { //text追加 var service = getService(); if (service.hasAccess()) { var url = 'https://api.twitter.com/1.1/statuses/update.json'; var payload = { status: text //textに変更 }; payload = Object.keys(payload).map(function(key) { return encodeRfc3986(key) + '=' + encodeRfc3986(payload[key]); }).join('&'); var response = service.fetch(url, { method: 'post', payload: payload, escaping: false }); 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); } } /** * Encodes a string using the RFC 3986 spec. */ function encodeRfc3986(str) { return encodeURIComponent(str).replace(/[!'()]/g, function(char) { return escape(char); }).replace(/\*/g, "%2A"); } /** * 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') // Set the property store where authorized tokens should be persisted. .setPropertyStore(PropertiesService.getUserProperties()); } /** * Handles the OAuth2 callback. */ function authCallback(request) { var service = getService(); var authorized = service.handleCallback(request); if (authorized) { return HtmlService.createHtmlOutput('Success!'); } else { return HtmlService.createHtmlOutput('Denied'); } }
Twitter投稿
function myFunction0() { run('お腹すいた'); }