1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { fetch } from "https://esm.town/v/std/fetch";
import { Buffer } from "node:buffer";
import process from "node:process";
// The account's 2FA needs to be disabled.
export async function getRedditAccessToken() {
const formData = new URLSearchParams();
formData.append("grant_type", "password");
formData.append("username", process.env.reddit_username);
formData.append("password", process.env.reddit_password);
const authorization = "Basic "
+ Buffer.from(
`${process.env.reddit_client_id}:${process.env.reddit_client_secret}`,
).toString("base64");
const response = await fetch("https://www.reddit.com/api/v1/access_token", {
method: "POST",
body: formData,
headers: {
"Authorization": authorization,
"Content-Type": "application/x-www-form-urlencoded",
},
});
return (await response.json()).access_token;
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023