Amy
Amy

Reputation: 59

npm request - custom HTTP Headers for twitch data

I have a assignment that getting the data from twitch API (Get Top Games). I used "request" module to connect to the twitch API. However, when I call the request, the terminal shows the status code is 401 because of the OAuth token is missing. I was wondering if there is an error in the headers of options object.

const request = require('request');

const options = {
  url: 'https://api.twitch.tv/helix/games/top',
  headers: {
    'User-Agent': 'myclientID'
  }
};

function callback(error, response, body) {
  console.log(response.statusCode)
  const info = JSON.parse(body);
  console.log(info)

}

request(options, callback);

Upvotes: 0

Views: 406

Answers (1)

gaganshera
gaganshera

Reputation: 2639

As per the twitch API docs, the client-id should be sent in separate Client-ID header, not under the User-Agent. Also, you need to pass the authorization token (App Access Token or User OAuth Token)

curl -H 'Client-ID: uo6dggojyb8d6soh92zknwmi5ej1q2' \
-H 'Authorization: Bearer cfabdegwdoklmawdzdo98xt2fo512y' \
-X GET 'https://api.twitch.tv/helix/games/top'

In node.js request format, is should be something like this:

var request = require('request');
var options = {
  'method': 'GET',
  'url': 'https://api.twitch.tv/helix/games/top',
  'headers': {
    'Client-ID': 'uo6dggojyb8d6soh92zknwmi5ej1q2',
    'Authorization': 'Bearer cfabdegwdoklmawdzdo98xt2fo512y'
  }
};
request(options, function (error, response) { 
  if (error) throw new Error(error);
  console.log(response.body);
});

Upvotes: 1

Related Questions