Getting Channel ID via 'users?login' not working

Currently developing a Node bot and working on a function to post in Discord whenever I go live.

From what I have read, the only way to get a channel ID is doing the ‘users?login’ method, such as this:

function twitchLiveCheck(){
  let options = {
    hostname: 'api.twitch.tv',
    family: 4,
    pathname: '/kraken/users?login=darkswordsmantv',
    headers: {
      'Accept': 'application/vnd.twitchtv.v5+json',
      'Client-ID': '~~~~'
    }
  }

  console.log('sending twitch request');

  https.get(options, function(res){
    console.log('response received');

    let data;

    res.on('data', function(chunk){
      console.log('adding data');
      data += chunk;
    });

    res.on('end', function(){
      console.log('data stream ended');
      try {
        const parsedData = data;
        console.log(parsedData);
      } catch (e) {
        console.error("error: " + e.message);
      }
    });
  });  
}

This returns an HTML response of the following. I originally had const parsedData = JSON.parse(data), but that wasn’t working for obvious reasons. This also includes the console messages:

sending twitch request
Server is listening on port: 1337
response received
adding data
data stream ended
undefined<html>
  <head><title>302 Found</title></head>
  <body bgcolor="white">
    <center><h1>302 Found</h1></center>
    <hr><center>nginx</center>
  </body>
</html>

From my understanding it’s supposed to return a JSON response with my client ID and other information.

I’m just trying to get information to tell if I’m live so I can send a Discord message.

I am using express v5.6.0 and Node v8.9.3.

Assuming the the “https” module, pathname should just be path.

So first, I realized I have const https = require('http') which is wrong and probably resulted in this after changing to path:

undefined{"error":"Bad Request","status":400,"message":"Requests must be made over SSL"}

However, when I changed it to https, it came out with this error after waiting for about 10 seconds:

Error: socket hang up

The follows works in my quick test on v8.9.4:

var https = require('https')

function twitchLiveCheck(){
  let options = {
    hostname: 'api.twitch.tv',
	family: 4,
    path: '/kraken/users?login=darkswordsmantv',
    headers: {
      'Accept': 'application/vnd.twitchtv.v5+json',
      'Client-ID': ''
    }
  }

  console.log('sending twitch request');

  https.get(options, function(res){
    console.log('response received');

    let data = "";

    res.on('data', function(chunk){
      console.log('adding data');
      data += chunk;
    });

    res.on('end', function(){
      console.log('data stream ended');
      try {
        const parsedData = data;
        console.log(parsedData);
      } catch (e) {
        console.error("error: " + e.message);
      }
    });
  });  
}

twitchLiveCheck()

and results in:

C:\Users\....\Desktop\test>node dark.js
sending twitch request
response received
adding data
data stream ended
{"_total":1,"users":[{"display_name":"DarkSwordsmanTV","_id":"48971983","name":"darkswordsmantv","type":"user","bio":"Bio? Like Biology? I mean I guess...","created_at":"2013-09-14T13:53:29.819638Z","updated_at":"2018-02-07T00:51:16.807901Z","logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/darkswordsmantv-profile_image-a0be732acd45b0c2-300x300.png"}]}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.