NodeJS - Get json with node-fetch

Hi! I am having trouble understanding how to use the urls that make up the twitch api. Say I want to get the json data from https://api.twitch.tv/helix/streams/ (My end goal is to make a !isLive command in discord.js but if this works that shouldn’t be hard). I was playing around with node-fetch but I am kind of stuck. Here is what I have so far:

const fetch = require('node-fetch')
fetch('https://api.twitch.tv/helix/streams/', {
    method: 'GET',
    headers: {
//not sure what goes here, i have tried multiple things
    }
})
 .then(res => res.json())
 .then(res => {
     console.log(res) // I keep getting "OAuth token is missing"
 });

Say my OAuth token is oauth:thisismytokenyoucannothaveit (the one you get from https://twitchapps.com/tmi) and my Client ID is thisismyclientidyoucannothaveit (The one you get from making an app on https://dev.twitch.tv/console/). How would I incorporate these into headers: {} (or wherever) so I get the desired result? Also is this the right URL to get stream information?

the headers should be

headers: {
    'Client-ID': theClientId,
    'Authorization': 'Bearer ' + theToken
}

Since you have used twitchapps token generator, you’ll need their clientID and theToken should have oauth: removed from the start.

You also need to update the URL to include the username or ID or the channel you wish to lookup, otherwise right now you’ll just get the first/top 20 streams on the platform.

You cannot use, your clientID and a token generated from someone elses tool.

Ideally you should generate and use your own token.

An app access token would suffice

Okay thanks! I will try that later

Hmm… I made a fresh app and got the client id from there, then got a fresh token, and I get something other than “OAuth token is missing” (yay!). However I still get “ClientID and OAuth token do not match”. Any ideas?

Also what should I add to the url to specify one channel? My username is komali09

I’m sorry if these are obvious answers but I have no clue

The URL would be

https://api.twitch.tv/helix/streams?user_login=komali09

as per the docs

This suggests you didn’t use the ClientID that the oAuth token was generated with

Sorry, but I don’t understand… am I supposed to do something with the OAuth token when making the application? I don’t see a spot except for “OAuth Redirect URLs” and I thought that was supposed to be https://localhost:xxxx or whatever website we are using (I’m using localhost)

Use the ClientID and clientSecret to generate an App Access Token

From what you wrote it sounds like you tried to use your Client Secret as a token in error

Ok I am still confused
I was using the string found under “Client ID” in the application menu (“Client Secret” is under that)
Also where do I put the POST command? It doesn’t work in command prompt and I’m confused where to put things like that

Ideally you should create an App access token and use it until it expires.

For example

Will generate a token and store it in Redis.

Your code above (node-fetch) can load the token from Redis and use it.

However you can do (not tested not sure of correct node-fetch syntax to send a form). This is not advised as it generates a token every time

const fetch = require('node-fetch')
fetch('https://id.twitch.tv/oauth2/token', {
    method: 'POST',
    body: {
        client_id: client_id,
        client_secret: client_secret,
        grant_type: "client_credentials"
    }
})
.then(res => res.json())
.then(res => {
    var token = resp.body.access_token

fetch('https://api.twitch.tv/helix/streams/', {
    method: 'GET',
    headers: {
        'Client-ID': yourID
        'Authorization': 'Bearer' + token
    }
})
 .then(res => res.json())
 .then(res => {
     console.log(res) // I keep getting "OAuth token is missing"
 });

});

Okay; I tried that (thanks for all your help)
It says “resp is undefined”. Was that a typo?

Yeah I was copy/pasting from my example and tweaking for node fetch (I prefer got over node fetch)

So what should I use instead?

node-fetch is fine I prefer got

as used in the oAuth example

Sorry, I mean instead of resp.body.access_token so I don’t get the error

res.body.access_token

Hmm… I assumed so, so I tried that beforehand. I still get cannot read property "access_token" of undefined

Like I said I don’t use node-fetch and I didn’t test this for you, you should be able to basic debug this!

Change

.then(res => {
    var token = resp.body.access_token

to

.then(res => {
    console.log(res);

and see what it returns for yourself.

I imagine it should be in fact

    var token = res.access_token;

Ok; sorry for being confusing.
It says “missing client id” but i think i can work that out myself

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