-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetToken.js
39 lines (34 loc) · 1.08 KB
/
getToken.js
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
/**
* Get an SFMC REST API token
* @param {{
* client_id: string,
* client_secret: string
* }} auth Client id/secret to use for the token request
* @param {string?} mid Business unit MID; only included in the token request if available; falls back to private var `utilMID` if empty.
* @returns {object | undefined}
*/
function getToken(auth, mid) {
if (!auth || !auth.client_id || !auth.client_secret) {
return undefined
}
// This constant is defined ./_private.js
var authEndpoint = AUTH_BASE_SFMC
var payload = {
client_id: auth.client_id,
client_secret: auth.client_secret,
grant_type: 'client_credentials'
}
if (mid || utilMID) {
payload.account_id = mid ? mid : utilMID
}
var accessTokenRequest = HTTP.Post(authEndpoint, 'application/json', Stringify(payload))
if (accessTokenRequest.StatusCode == 200) {
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0])
return {
token: tokenResponse.access_token,
restInstanceURL: tokenResponse.rest_instance_url
}
} else {
return undefined
}
}