Wednesday 26 April 2017

Twitch POST request for OAuth2 token

I'm trying to get an access token via a POST request from the Twitch API but my http.request is never executing (I don't think) and I can't figure out why.

var express               = require('express');
var path                  = require('path');
var bodyParser            = require('body-parser');
var fs                    = require('fs');
var https                 = require('https');
var querystring           = require('querystring');
var authInfo              = require('./authInfo.json');

app.get('/twitch/auth', function(req, res) {
  res.send("auth page");

  var data = querystring.stringify({
    client_id: authInfo.clientID,
    client_secret: authInfo.clientSecret,
    grant_type: "authorization_code",
    redirect_uri: authInfo.redirectURI,
    code: req.query.code,
    state: 12345
  });

  var options = {
    host: 'api.twitch.tv',
    port: 443,
    path: '/kraken/oauth2/token',
    method: 'POST',
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Content-Length': Buffer.byteLength(data)
    }
  }

  var req = https.request(options, (res) => {
    console.log('statusCode:', res.statusCode);
    console.log('headers:', res.headers);

    res.on('data', (d) => {
      console.log(d);
    });
  });

  req.on('error', (e) => {
    console.log(e);
  });

  req.end();

});

app.listen(port, function() {
  console.log('Point browser to: http://localhost:' + port);
});

PS I've omitted the some code for brevity but I get back an authorization code successfully.



via Atonium

No comments:

Post a Comment