1 | |
2 | |
3 | * Request Client Access |
4 | * (For partnerships only) Link Paychex clients to a 3rd party partner application |
5 | */ |
6 | export async function main(auth: RT.Paychex, body: Body) { |
7 | const accessToken = await getOAuthToken(auth, 'https://api.paychex.com/auth/oauth/v2/token') |
8 | const url = new URL(`https://api.paychex.com/management/requestclientaccess`) |
9 |
|
10 | const response = await fetch(url, { |
11 | method: 'POST', |
12 | headers: { |
13 | 'Content-Type': 'application/json', |
14 | Authorization: 'Bearer ' + accessToken |
15 | }, |
16 | body: JSON.stringify(body) |
17 | }) |
18 | if (!response.ok) { |
19 | const text = await response.text() |
20 | throw new Error(`${response.status} ${text}`) |
21 | } |
22 | return await response.json() |
23 | } |
24 |
|
25 | async function getOAuthToken(auth: RT.Paychex, tokenUrl: string): Promise<string> { |
26 | const params = new URLSearchParams({ |
27 | grant_type: 'client_credentials' |
28 | }) |
29 |
|
30 | const response = await fetch(tokenUrl, { |
31 | method: 'POST', |
32 | headers: { |
33 | Authorization: 'Basic ' + btoa(`${auth.client_id}:${auth.client_secret}`), |
34 | 'Content-Type': 'application/x-www-form-urlencoded' |
35 | }, |
36 | body: params.toString() |
37 | }) |
38 |
|
39 | if (!response.ok) { |
40 | const text = await response.text() |
41 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
42 | } |
43 |
|
44 | const data = await response.json() |
45 | return data.access_token |
46 | } |
47 |
|
48 | |
49 | |
50 | * This file was automatically generated by json-schema-to-typescript. |
51 | * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file, |
52 | * and run json-schema-to-typescript to regenerate this file. |
53 | */ |
54 |
|
55 | export interface Body { |
56 | |
57 | * The client account number used for identification purposes. (no dash or spaces allowed, only the last 8 characters of the ID) |
58 | */ |
59 | displayId: string |
60 | [k: string]: unknown |
61 | } |
62 |
|