Avatar

@mschleske

2 public vals
Joined January 11, 2023
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
import { fetch } from "https://esm.town/v/std/fetch";
import { Buffer } from "node:buffer";
export const authenticateReddit = async (
CLIENT_ID,
CLIENT_SECRET,
ACCESS_CODE,
REDIRECT_URI,
) => {
const headers = {};
headers["Authorization"] = `Basic ${
Buffer.from(
`${CLIENT_ID}:${CLIENT_SECRET}`,
).toString("base64")
}`;
headers["Content-Type"] = "application/x-www-form-urlencoded";
const formData = new URLSearchParams();
formData.append("grant_type", "authorization_code");
formData.append("code", ACCESS_CODE);
formData.append("redirect_uri", REDIRECT_URI);
let response;
try {
response = await fetch("https://www.reddit.com/api/v1/access_token", {
headers: headers,
method: "POST",
body: formData,
});
} catch (e) {
console.error(e);
return;
}
const body = await response.json();
return body;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { authenticateReddit } from "https://esm.town/v/mschleske/authenticateReddit";
export const fetchRedditToken = async (
CLIENT_ID,
CLIENT_SECRET,
ACCESS_CODE,
REDIRECT_URI
) => {
const response = await authenticateReddit(
CLIENT_ID,
CLIENT_SECRET,
ACCESS_CODE,
REDIRECT_URI
);
return response.access_token;
};
Next