-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathofferClass.js
More file actions
68 lines (57 loc) · 1.81 KB
/
offerClass.js
File metadata and controls
68 lines (57 loc) · 1.81 KB
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
var https = require('https');
const {google} = require('googleapis');
var key = require("./privatekey.json");
exports.saveClass = function (classData) {
const postData = classData;//JSON.stringify(classData);
const options = {
// protocol: 'https',
hostname: 'www.googleapis.com',
path: '/walletobjects/v1/offerClass?strict=true',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
};
const req = https.request(options, (res) => {
console.log(`STATUS: ${res.statusCode}`);
console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log(`BODY: ${chunk}`);
});
res.on('end', () => {
console.log('No more data in response.');
});
});
req.on('error', (e) => {
console.error(`problem with request: ${e.message}`);
});
getAuth().then(token => {
// options.auth = {'Bearer' : token.token };
// options.headers.Authorization = "Bearer " + token.token;
req.setHeader('Authorization', "Bearer " + token.token);
// write data to request body
req.write(postData);
req.end();
}).catch(error => {
console.error;
});
};
function getAuth() {
const jwtClient = new google.auth.JWT({
email: key.client_email,
key: key.private_key,
scopes: ['https://www.googleapis.com/auth/wallet_object.issuer']
});
return new Promise(function(resolve, reject) {
jwtClient.getAccessToken()
.then(response => {
console.log(response);
resolve(response);
}).catch(error => {
console.error;
reject(error);
});
});
}