-2

Hello everyone! I need to send http get request to twitch API. How it works: User inputs name of the streamer, my programm sends http get request to twitch API and the output need to be how much viewers right now on the twitch stream. my try:

import fetch from 'node-fetch';

const response = await fetch('https://www.google.com/');
const data = await response.json();

console.log(data);
Danylo
  • 3
  • 4

1 Answers1

0

I recommend that you use a package like axios to make your request. That is because to authenticate you need to send a POST requeust as well which axios makes very easy.

First you need to authenticate to the server this can be done like so

axios.post('https://id.twitch.tv/oauth2/token', {
    client_id: '<your client id>',
    client_secret: '<your client id>',
    grant_type: 'client_credentials'
  })
  .then(function (response) {
    console.log(response);

// the response will look like this
// save the access token you will need it for every request you send
/*
{
  "access_token": "jostpf5q0puzmxmkba9iyug38kjtg",
  "expires_in": 5011271,
  "token_type": "bearer"
}
*/
  })

You can query for a channel like this. You can find all request you can make and their response here. Again here you need to provide the authentication from the previous step

axios.get('https://api.twitch.tv/helix/search/channels?query=loserfruit', {
    headers: {
      'Client-Id': '<your client id>',
      'Authorization': 'Bearer <access_token fron previous request>'
}})
  .then(function (response) {
    console.log(response);
  })

For the example you provided it would look like this (you did not include the Bearer Prefix

axios.get('https://api.twitch.tv/helix/search/channels?query=loserfruit', {
    headers: {
      'Client-Id': 'mxciemz4ew',
      'Authorization': 'Bearer vz9fcq1xv0qxxr7kcr2g9btubgdof'
}})
  .then(function (response) {
    console.log(response);
  })
Vincent Menzel
  • 1,040
  • 1
  • 6
  • 17
  • I think your code is correct but when I started the application it gave me an error: `[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "AxiosError: Request failed with status code 401".] { code: 'ERR_UNHANDLED_REJECTION' } ` – Danylo Aug 31 '22 at 21:14
  • Did you provide the correct Client Id and Authorization? You will get a 401 if you are not authorized to do a request or did you get the response when trying to authenticate – Vincent Menzel Sep 01 '22 at 13:45
  • do you know where i can get the client id? – Danylo Sep 02 '22 at 09:50
  • You get the clientId when you register your application with twitch See https://dev.twitch.tv/docs/authentication/register-app – Vincent Menzel Sep 02 '22 at 10:35
  • What do you mean with throw, set or get a request? If you dont want to interact with twitch server why do you try to use their api? – Vincent Menzel Sep 02 '22 at 20:22
  • Sorry, I didn’t understand, I was wrong. Now I’m understand – Danylo Sep 02 '22 at 22:46
  • client id and client client secret I found but what do I need to do in these fields `grant_type: 'client_credentials'`, access token I already have – Danylo Sep 03 '22 at 19:11
  • I edited the question, and there asked did I do everything right or not? if not difficult please answer – Danylo Sep 03 '22 at 20:38
  • Your auth would be `'Authorization': 'Bearer vz9fcq1xv0qxxr7kcr2g9btubgdof'` (I updated my answer) – Vincent Menzel Sep 03 '22 at 22:51
  • and what is grant_type and where to get it? – Danylo Sep 04 '22 at 18:09
  • Can you update your question and show me what it should look like, with my client ID and so on. I generated the token myself, I wrote a python script – Danylo Sep 05 '22 at 09:59
  • @Danylo `grant_type: 'client_credentials'` is according to the docs what you need to pass to the endpoint to get authenticate. They probably offer additional Authentication methods which can be used instead. When you use it like in my example you dont need to change anything about it. For my example you only need to change whats in between `<...>` – Vincent Menzel Sep 05 '22 at 11:47
  • Thanks ! everything is right! – Danylo Sep 05 '22 at 14:53