twitch-api

Why is the twitch api not retuning anything?


I have a problem with this code :

const getToken = async () => {
          const tokenResponse = await fetch(
            `https://id.twitch.tv/oauth2/token?client_id=${ID}&client_secret=${SECRET}&grant_type=${TYPE}`, 
            {
              method: "POST",
            }
          );
        
          const tokenJson = await tokenResponse.json();
          const token = tokenJson.access_token;
        
          return token;
        };
        
        const getData = async () => {
          const url = `https://api.twitch.tv/helix/users?login=xqcow`
          const token = await getToken();
        
          
          const res = await fetch(url, {
            method: "GET",
            headers: {
              "client-id": ID,
              "Authorization": `Bearer ${token}`,
            }
          })
        
          console.log(res)
        }
        
        getData();

I got a status 200 but it is not returning the info about the user and I don't get why.

What am I missing ? Is this the normal behaviour of the twitch api ?

Please help i'm struggling a little bit with this problem. I checked the documentation but it doesn't really help me.

Thanks


Solution

  • Your fetch code is incomplete

    Your getData should be more like

            const getData = async () => {
              const url = `https://api.twitch.tv/helix/users?login=xqcow`
              const token = await getToken();
            
              
              const res = await fetch(url, {
                method: "GET",
                headers: {
                  "client-id": ID,
                  "Authorization": `Bearer ${token}`,
                }
              })
              let twitch_data = await res.json();
              let user_data = twitch_data.data[0];
              console.log(user_data);
            }