Created
August 5, 2020 09:06
-
-
Save aikatsukamen/b06dfac0199f78209413773dad6ca85b to your computer and use it in GitHub Desktop.
TimeTreeの招待リンクを取得する
This file contains hidden or 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
| // ライブラリの登録が必要 | |
| // 13KJxU8q0ZYmZXyQswU2HrkQX-yXlgnlJ3BVzsKrS69oaE4FcViPRFPZb | |
| // スクリプトプロパティの設定が必要 | |
| // TIMETREE_MAIL: TimeTreeのログインメールアドレス | |
| // TIMETREE_PASSWORD: TimeTreeのログインパスワード | |
| /** TimeTreeの招待リンクを取得する */ | |
| function fetchTimeTreeInviteLink(){ | |
| const calendarId = '4252315'; // kktカレンダー | |
| const loginMail = PropertiesService.getScriptProperties().getProperty('TIMETREE_MAIL'); | |
| const loginPassword = PropertiesService.getScriptProperties().getProperty('TIMETREE_PASSWORD'); | |
| // ライブラリ: 13KJxU8q0ZYmZXyQswU2HrkQX-yXlgnlJ3BVzsKrS69oaE4FcViPRFPZb | |
| const cheerio = libpack.cheerio(); | |
| let $; | |
| console.log('サインインのページ開いてCSRFのトークンを取得'); | |
| let url = 'https://timetreeapp.com/signin'; | |
| console.log(url); | |
| let response = UrlFetchApp.fetch('https://timetreeapp.com/signin'); | |
| let content = response.getContentText("UTF-8") | |
| $ = cheerio.load(content); | |
| let token = $("meta[name='csrf-token']").attr("content"); | |
| console.log('token: ' + token); | |
| console.log('サインイン'); | |
| let headers = { | |
| 'X-TimeTreeA': 'web/2.0.0/ja', | |
| 'X-CSRF-Token': token, | |
| }; | |
| let payload = { | |
| password: loginPassword, | |
| uid: loginMail, | |
| uuid: '8e8ae388246547dfa0baebb1276aee1a' // UUIDのハイフン抜き形式なら何でもいいっぽい | |
| }; | |
| let options = { | |
| 'method': 'put', | |
| 'headers': headers, | |
| 'payload': payload, | |
| 'followRedirects': false | |
| } | |
| url = 'https://timetreeapp.com/api/v1/auth/email/signin'; | |
| console.log(url); | |
| console.log(JSON.stringify(options, null, ' ')); | |
| response = UrlFetchApp.fetch(url, options) | |
| let cookies = response.getHeaders()["Set-Cookie"]; | |
| console.log('cookie: ' + cookies); | |
| // cookieを付与してカレンダー一覧のページ取得。新しいトークンだけもらう。 | |
| console.log('新しいトークン取得'); | |
| headers = { | |
| Cookie: cookies | |
| }; | |
| options = { | |
| 'method': 'get', | |
| 'headers': headers, | |
| 'followRedirects': true | |
| } | |
| url = 'https://timetreeapp.com/calendars'; | |
| console.log(url); | |
| console.log(JSON.stringify(options, null, ' ')); | |
| response = UrlFetchApp.fetch(url, options) | |
| content = response.getContentText("UTF-8") | |
| $ = cheerio.load(content); | |
| token = $("meta[name='csrf-token']").attr("content"); | |
| console.log('token: ' + token); | |
| // 招待リンク取得 | |
| console.log('招待リンク取得'); | |
| headers = { | |
| 'X-TimeTreeA': 'web/2.0.0/ja', | |
| 'X-CSRF-Token': token, | |
| Cookie: cookies | |
| }; | |
| options = { | |
| 'method': 'get', | |
| 'headers': headers, | |
| 'followRedirects': false | |
| }; | |
| url = 'https://timetreeapp.com/api/v1/calendar/' + calendarId + '/invite/signature'; | |
| console.log(url); | |
| console.log(JSON.stringify(options, null, ' ')); | |
| response = UrlFetchApp.fetch(url, options); | |
| const result = JSON.parse(response.getContentText()); | |
| console.log('--招待リンクのレスポンス--'); | |
| console.log(JSON.stringify(result, null, ' ')); | |
| //{ | |
| // "url": "https://timetr.ee/s/hogehogehoge", | |
| // "signature": "よくわかんない文字列", | |
| // "expire_date": 1599207034590 | |
| //} | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment