-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetAccessToken.js
55 lines (43 loc) · 1.29 KB
/
getAccessToken.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const https = require('https');
const fs = require('fs');
const jwt = require('jsonwebtoken');
const serviceAccount = require('./user-firebase.json');
const getAccessToken = () => {
const now = Math.floor(Date.now() / 1000);
const privateKey = serviceAccount.private_key;
const payload = {
iss: serviceAccount.client_email,
sub: serviceAccount.client_email,
scope: 'https://www.googleapis.com/auth/firebase.messaging',
aud: 'https://oauth2.googleapis.com/token',
iat: now,
exp: now + 3600
};
const token = jwt.sign(payload, privateKey, { algorithm: 'RS256' });
const postData = `grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=${token}`;
const options = {
hostname: 'oauth2.googleapis.com',
path: '/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const accessToken = JSON.parse(data).access_token;
console.log('Access Token:', accessToken);
});
});
req.on('error', (e) => {
console.error('Error:', e);
});
req.write(postData);
req.end();
};
getAccessToken();