summarylogtreecommitdiffstats
path: root/token-server.js
blob: 5587670f1b31802aba9aee169b0129688af192c8 (plain)
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
// Simple token server for Twitch App Access Token
// Usage: set environment variables CLIENT_ID and CLIENT_SECRET, then
//   node token-server.js

const http = require('http');
const https = require('https');
const url = require('url');

const PORT = process.env.PORT || 3000;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;

if (!CLIENT_ID || !CLIENT_SECRET) {
  console.error('Please set CLIENT_ID and CLIENT_SECRET environment variables');
  process.exit(1);
}

function fetchAppToken(callback) {
  const postPath = `/oauth2/token`;
  const postBody = `client_id=${encodeURIComponent(CLIENT_ID)}&client_secret=${encodeURIComponent(CLIENT_SECRET)}&grant_type=client_credentials`;
  const opts = {
    hostname: 'id.twitch.tv',
    path: postPath,
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(postBody)
    }
  };

  const req = https.request(opts, (res) => {
    let data = '';
    res.on('data', (chunk) => data += chunk);
    res.on('end', () => {
      // Log status for easier debugging
      console.log('Twitch token response status:', res.statusCode);
      console.log('Twitch token response body:', data);
      try {
        const parsed = JSON.parse(data);
        callback(null, parsed);
      } catch (e) {
        callback(e || new Error('Failed to parse response'));
      }
    });
  });

  req.on('error', (e) => callback(e));
  req.write(postBody);
  req.end();
}

const server = http.createServer((req, res) => {
  const u = url.parse(req.url, true);
  if (u.pathname === '/twitch-token') {
    fetchAppToken((err, body) => {
      if (err) {
        res.writeHead(500, {'Content-Type': 'application/json'});
        res.end(JSON.stringify({ error: 'token_fetch_failed', detail: String(err) }));
        return;
      }
      res.writeHead(200, {'Content-Type': 'application/json'});
      res.end(JSON.stringify(body));
    });
  } else {
    res.writeHead(404);
    res.end('Not found');
  }
});

server.listen(PORT, () => {
  console.log(`Token server listening on http://localhost:${PORT}`);
  console.log('Endpoint: /twitch-token');
});