-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathSfmcApiHelper.ts
175 lines (155 loc) · 6.76 KB
/
SfmcApiHelper.ts
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
'use strict';
import axios from 'axios';
import express = require("express");
import Utils from './Utils';
export default class SfmcApiHelper
{
// Instance variables
private _deExternalKey = "DF18Demo";
private _sfmcDataExtensionApiUrl = "https://www.exacttargetapis.com/hub/v1/dataevents/key:" + this._deExternalKey + "/rowset";
/**
* getOAuthAccessToken: POSTs to SFMC Auth URL to get an OAuth access token with the given ClientId and ClientSecret
*
* More info: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-getting-started.meta/mc-getting-started/get-access-token.htm
*
*/
public getOAuthAccessToken(clientId: string, clientSecret: string) : Promise<any>
{
let self = this;
Utils.logInfo("getOAuthAccessToken called.");
Utils.logInfo("Using specified ClientID and ClientSecret to get OAuth token...");
let headers = {
'Content-Type': 'application/json;charset=UTF-8',
};
let postBody = {
'clientId': clientId,
'clientSecret': clientSecret
};
return self.getOAuthTokenHelper(headers, postBody);
}
/**
* getOAuthAccessTokenFromRefreshToken: POSTs to SFMC Auth URL to get an OAuth access token with the given refreshToken
*
* More info: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-getting-started.meta/mc-getting-started/get-access-token.htm
*
*/
public getOAuthAccessTokenFromRefreshToken(clientId: string, clientSecret: string, refreshToken: string) : Promise<any>
{
let self = this;
Utils.logInfo("getOAuthAccessTokenFromRefreshToken called.");
Utils.logInfo("Getting OAuth Access Token with refreshToken: " + refreshToken);
let headers = {
'Content-Type': 'application/json;charset=UTF-8',
};
let postBody = {
'clientId': clientId,
'clientSecret': clientSecret,
'refreshToken': refreshToken
};
return self.getOAuthTokenHelper(headers, postBody);
}
/**
* getOAuthTokenHelper: Helper method to POST the given header & body to the SFMC Auth endpoint
*
*/
public getOAuthTokenHelper(headers : any, postBody: any) : Promise<any>
{
return new Promise<any>((resolve, reject) =>
{
// POST to Marketing Cloud REST Auth service and get back an OAuth access token.
let sfmcAuthServiceApiUrl = "https://auth.exacttargetapis.com/v1/requestToken";
axios.post(sfmcAuthServiceApiUrl, postBody, {"headers" : headers})
.then((response: any) => {
// success
let accessToken = response.data.accessToken;
let tokenExpiry = new Date();
tokenExpiry.setSeconds(tokenExpiry.getSeconds() + response.data.expiresIn);
Utils.logInfo("Got OAuth token: " + accessToken + ", expires = " + tokenExpiry);
resolve(
{
oauthAccessToken: accessToken,
oauthAccessTokenExpiry: tokenExpiry,
status: response.status,
statusText: response.statusText + "\n" + Utils.prettyPrintJson(JSON.stringify(response.data))
});
})
.catch((error: any) => {
// error
let errorMsg = "Error getting OAuth Access Token.";
errorMsg += "\nMessage: " + error.message;
errorMsg += "\nStatus: " + error.response ? error.response.status : "<None>";
errorMsg += "\nResponse data: " + error.response ? Utils.prettyPrintJson(JSON.stringify(error.response.data)) : "<None>";
Utils.logError(errorMsg);
reject(errorMsg);
});
});
}
/**
* loadData: called by the GET handlers for /apidemoloaddata and /appdemoloaddata
*
*/
public loadData(req: express.Request, res: express.Response)
{
let self = this;
let sessionId = req.session.id;
Utils.logInfo("loadData entered. SessionId = " + sessionId);
if (req.session.oauthAccessToken)
{
Utils.logInfo("Using OAuth token: " + req.session.oauthAccessToken);
self.loadDataHelper(req.session.oauthAccessToken, req.session.sampleJsonData)
.then((result) => {
res.status(result.status).send(result.statusText);
})
.catch((err) => {
res.status(500).send(err);
});
}
else
{
// error
let errorMsg = "OAuth Access Token *not* found in session.\nPlease complete previous demo step\nto get an OAuth Access Token.";
Utils.logError(errorMsg);
res.status(500).send(errorMsg);
}
}
/**
* loadDataHelper: uses the given OAuthAccessToklen to load JSON data into the Data Extension with external key "DF18Demo"
*
* More info: https://developer.salesforce.com/docs/atlas.en-us.noversion.mc-apis.meta/mc-apis/postDataExtensionRowsetByKey.htm
*
*/
private loadDataHelper(oauthAccessToken: string, jsonData: string) : Promise<any>
{
let self = this;
Utils.logInfo("loadDataHelper called.");
Utils.logInfo("Loading sample data into Data Extension: " + self._deExternalKey);
Utils.logInfo("Using OAuth token: " + oauthAccessToken);
return new Promise<any>((resolve, reject) =>
{
let headers = {
'Content-Type': 'application/json;charset=UTF-8',
'Authorization': 'Bearer ' + oauthAccessToken
};
// POST to Marketing Cloud Data Extension endpoint to load sample data in the POST body
axios.post(self._sfmcDataExtensionApiUrl, jsonData, {"headers" : headers})
.then((response: any) => {
// success
Utils.logInfo("Successfully loaded sample data into Data Extension!");
resolve(
{
status: response.status,
statusText: response.statusText + "\n" + Utils.prettyPrintJson(JSON.stringify(response.data))
});
})
.catch((error: any) => {
// error
let errorMsg = "Error loading sample data. POST response from Marketing Cloud:";
errorMsg += "\nMessage: " + error.message;
errorMsg += "\nStatus: " + error.response ? error.response.status : "<None>";
errorMsg += "\nResponse data: " + error.response.data ? Utils.prettyPrintJson(JSON.stringify(error.response.data)) : "<None>";
Utils.logError(errorMsg);
reject(errorMsg);
});
});
}
}