Skip to content

Instantly share code, notes, and snippets.

@bowencool
Last active January 23, 2024 13:32
Show Gist options
  • Save bowencool/7da8630dafe9d07e7e004def2dcb851b to your computer and use it in GitHub Desktop.
Save bowencool/7da8630dafe9d07e7e004def2dcb851b to your computer and use it in GitHub Desktop.
dida365 oauth 滴答清单
// https://blog.bowen.cool/posts/how-to-forward-sms-to-your-todo-list
const Koa = require("koa");
const Router = require("@koa/router");
const { PORT = 4001 } = process.env;
const client_id = "xxx";
const client_secret = "xxx";
const scope = "tasks:write";
const redirect_uri = `http://localhost:${PORT}/redirect`;
const app = new Koa();
// // app.use(bodyParser());
// // router
const router = new Router();
router.get("/", async (ctx, next) => {
await next();
ctx.body = `<a href="https://dida365.com/oauth/authorize?scope=tasks:write&client_id=${client_id}&state=state&redirect_uri=${encodeURIComponent(
redirect_uri
)}&response_type=code">点击登录</a>`;
ctx.response.status = 200;
});
router.get("/redirect", async (ctx, next) => {
await next();
let code = ctx.query.code;
let buff = Buffer.from(`${client_id}:${client_secret}`);
let base64data = buff.toString("base64");
const response = await fetch("https://dida365.com/oauth/token", {
method: "POST",
headers: {
Authorization: `Basic ${base64data}`,
"Content-Type": `application/x-www-form-urlencoded`,
},
body: Object.entries({
code,
grant_type: "authorization_code",
scope,
redirect_uri,
})
.map(([key, value]) => {
return encodeURIComponent(key) + "=" + encodeURIComponent(value);
})
.join("&"),
});
const data = await response.text();
ctx.body = data;
ctx.response.status = response.status;
});
app.use(router.routes()).use(router.allowedMethods()).listen(PORT);
console.log(`listened http://localhost:${PORT}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment