-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.js
52 lines (40 loc) · 1.6 KB
/
authorization.js
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
"use strict"
const Mastodon = require("mastodon-api")
const readline = require("readline")
const fs = require("fs").promises
//書き出しファイルの名前
const outPutFileName = "authedId.json"
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
//readlineをpromise化
const question = (query) => new Promise((resolve,reject)=>{
rl.question(query,(text)=>{
resolve(text);
})
})
const baseUrl = 'https://mstdn.tamag.org'
!(async()=>{
//認証idを取得
const res = await Mastodon.createOAuthApp(baseUrl + '/api/v1/apps', 'おうち', 'read write follow')
.catch((err)=>console.err(err))
//認証URLを取得して認証コードをユーザー入力してもらう
const url = await Mastodon.getAuthorizationUrl(res.client_id, res.client_secret, baseUrl,'read write follow')
console.log('This is the authorization URL. Open it in your browser and authorize with your account!')
console.log(url)
const code = await question("認証コード:")
rl.close()
//アクセストークンを取得
const access_token = await Mastodon.getAccessToken(res.client_id, res.client_secret, code, baseUrl)
.catch((err)=>console.log(err))
const id_data = {
clientId : res.client_id,
clientSecret : res.client_secret,
accessToken: access_token
}
//書き込み
await fs.writeFile(outPutFileName,JSON.stringify(id_data,null," "))
.catch((err)=>console.log(err))
console.log(id_data)
})()