-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtweet.gs
More file actions
65 lines (59 loc) · 2.31 KB
/
tweet.gs
File metadata and controls
65 lines (59 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function postTweet(tweet){
const twitterService = getService();
if (twitterService.hasAccess()) {
// 投稿
var tweet = tweet + " - " + Utilities.formatDate(new Date(), "JST", "MMM d (E) h:mm a");
var sendOption = {
'method':"POST",
'payload':{status: tweet}
}
twitterService.fetch("https://api.twitter.com/1.1/statuses/update.json", sendOption);
} else {
Logger.log(twitterService.getLastError());
}
}
function postTweetWithImage(tweet, image){
const twitterService = getService();
if (twitterService.hasAccess()) {
var imageOption = {
'method':"POST",
'payload':{'media_data':image}
};
var image_upload = JSON.parse(twitterService.fetch("https://upload.twitter.com/1.1/media/upload.json",imageOption));
tweet = tweet + " - " + Utilities.formatDate(new Date(), "JST", "MMM d (E) h:mm a");
var sendOption = {
'method':"POST",
'payload':{status: tweet, 'media_ids':image_upload['media_id_string']}
}
twitterService.fetch("https://api.twitter.com/1.1/statuses/update.json", sendOption);
} else {
Logger.log(service.getLastError());
}
}
// 認証用URL取得
function getOAuthURL() {
Logger.log(getService().authorize());
}
// サービス取得
function getService() {
return OAuth1.createService('Twitter')
.setAccessTokenUrl('https://api.twitter.com/oauth/access_token')
.setRequestTokenUrl('https://api.twitter.com/oauth/request_token')
.setAuthorizationUrl('https://api.twitter.com/oauth/authorize')
// 設定した認証情報をセット
.setConsumerKey(PropertiesService.getScriptProperties().getProperty("CONSUMER_API_KEY"))
.setConsumerSecret(PropertiesService.getScriptProperties().getProperty("CONSUMER_API_SECRET"))
.setCallbackFunction('authCallback')
// 認証情報をプロパティストアにセット(これにより認証解除するまで再認証が不要になる)
.setPropertyStore(PropertiesService.getUserProperties());
}
// 認証成功時に呼び出される処理を定義
function authCallback(request) {
const service = getService();
const authorized = service.handleCallback(request);
if (authorized) {
return HtmlService.createHtmlOutput('success!!');
} else {
return HtmlService.createHtmlOutput('failed');
}
}