Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
import { basicAuthorization } from "https://esm.town/v/stevekrouse/basicAuthorization";
import { fetchJSON } from "https://esm.town/v/stevekrouse/fetchJSON";
export async function twitterRequestAccessToken(
{ code, redirect_uri, challenge, client_id, client_secret }: {
code: string;
redirect_uri: string;
challenge: string;
client_id: string;
client_secret: string;
},
) {
const url = "https://api.twitter.com/2/oauth2/token";
const body = new URLSearchParams({
code,
grant_type: "authorization_code",
code_verifier: challenge,
redirect_uri,
});
return fetchJSON(url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
...basicAuthorization(client_id, client_secret),
},
body: body.toString(),
});
}
November 7, 2023