1 | |
2 | type Paylocity = { |
3 | clientId: string |
4 | clientSecret: string |
5 | } |
6 | |
7 | * Get Callback |
8 | * > 🚧 Partner Restricted |
9 | > All assessment API endpoints are restricted to assessment providers that have signed a Paylocity technology partnership agreement. |
10 | */ |
11 | export async function main(auth: Paylocity, testMode?: string) { |
12 | const url = new URL( |
13 | `https://dc1prodgwext.paylocity.com/apiHub/performanceManagement/v1/companies/b6001/assessmentOrders/callback` |
14 | ) |
15 |
|
16 | const response = await fetch(url, { |
17 | method: 'GET', |
18 | headers: { |
19 | ...(testMode ? { testMode: testMode } : {}), |
20 | Authorization: |
21 | 'Bearer ' + |
22 | (await getOAuthToken(auth, 'https://dc1prodgwext.paylocity.com/public/security/v1/token')) |
23 | }, |
24 | body: undefined |
25 | }) |
26 | if (!response.ok) { |
27 | const text = await response.text() |
28 | throw new Error(`${response.status} ${text}`) |
29 | } |
30 | return await response.json() |
31 | } |
32 |
|
33 | async function getOAuthToken(auth: Paylocity, tokenUrl: string): Promise<string> { |
34 | const params = new URLSearchParams({ |
35 | grant_type: 'client_credentials', |
36 | client_id: auth.clientId, |
37 | client_secret: auth.clientSecret |
38 | }) |
39 |
|
40 | const response = await fetch(tokenUrl, { |
41 | method: 'POST', |
42 | headers: { |
43 | Authorization: 'Basic ' + btoa(`${auth.clientId}:${auth.clientSecret}`), |
44 | 'Content-Type': 'application/x-www-form-urlencoded' |
45 | }, |
46 | body: params.toString() |
47 | }) |
48 |
|
49 | if (!response.ok) { |
50 | const text = await response.text() |
51 | throw new Error(`OAuth token request failed: ${response.status} ${text}`) |
52 | } |
53 |
|
54 | const data = await response.json() |
55 | return data.access_token |
56 | } |
57 |
|