From 1a2c4186293eaf58103f99c7b2ad664888bcde1a Mon Sep 17 00:00:00 2001 From: winsdominoes Date: Mon, 8 Jul 2024 21:36:36 +0700 Subject: [PATCH] misskey post api --- .gitignore | 4 ++++ config/token.json.example | 3 +++ src/index.js | 36 +++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 config/token.json.example diff --git a/.gitignore b/.gitignore index ceaea36..b8ca4d5 100644 --- a/.gitignore +++ b/.gitignore @@ -130,3 +130,7 @@ dist .yarn/install-state.gz .pnp.* +# custom config +config/*.json + + diff --git a/config/token.json.example b/config/token.json.example new file mode 100644 index 0000000..2671f46 --- /dev/null +++ b/config/token.json.example @@ -0,0 +1,3 @@ +{ + "token": "" +} \ No newline at end of file diff --git a/src/index.js b/src/index.js index e4e00f5..a894b9f 100644 --- a/src/index.js +++ b/src/index.js @@ -53,23 +53,45 @@ async function get_access_token(host, session_id) { }); const data = await response.json(); + console.log(data); const res_status = data.ok; - const res_token = data.token; - - if(res_status) { - return res_token; + if(data.ok == true) { + return data.token; } else { return false; // error occured } }; (async () => { - await check_auth_status() - let access_token = await get_access_token(host, session_id); + let access_token = 0; + const token_file = JSON.parse(fs.readFileSync(path.resolve("config", "token.json"))); + if(!token_file.token || token_file.token == "" || token_file.token == false) { // if existing token not exist + await check_auth_status() + access_token = await get_access_token(host, session_id); + + let json_data = { + "token": access_token + } + + fs.writeFileSync( + path.resolve("config", "token.json"), + JSON.stringify(json_data) + ); + } else { + access_token = token_file.token; + } - console.log(access_token); + const cli = new Misskey.api.APIClient({ + origin: host, + credential: access_token + }); + + const meta = await cli.request('notes/create', { + text: "hi from misskey api!" + }); + console.log(meta); })(); app.listen(port);