0

Generate OAuth access token

by
Published Dec 20, 2024

Generate an OAuth access token by using an authorization code or a refresh token.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Generate OAuth access token
7
 * Generate an OAuth access token by using an authorization code or a refresh token.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  body: {
12
    grant_type: "authorization_code" | "refresh_token" | "client_credentials";
13
  },
14
) {
15
  const url = new URL(`https://api.pinterest.com/v5/oauth/token`);
16

17
  const response = await fetch(url, {
18
    method: "POST",
19
    headers: {
20
      "Content-Type": "application/x-www-form-urlencoded",
21
      Authorization: "Bearer " + auth.token,
22
    },
23
    body: new URLSearchParams(body as Record<string, string>),
24
  });
25
  if (!response.ok) {
26
    const text = await response.text();
27
    throw new Error(`${response.status} ${text}`);
28
  }
29
  return await response.json();
30
}
31