OAuth Token is Missing (discord.js)

I’ve created a custom bot for a streamer but I started getting

{

    error: 'Unauthorized',

    status: 401,

    message: 'OAuth token is missing'

}

but before that I was able to get data.

Here’s my code that does the HTTPS Request

function callTwitchApi()
{
    let options = {
        hostname: `api.twitch.tv`,
        path: `/helix/streams?user_login=ubaeph`,
        method: 'GET',
        headers: {
            'cliend_id': config.twitchClientID,
            'client_secret': config.twitchClientSecret,
            'token_type': 'bearer'
        },
        responseType: 'json' 
    };
  
    https.get(options, (res) => {
        let body = '';
      
        res.on ('data', (chunk) => {
            body += chunk;
        });
      
        res.on('end', () => {
            let json;
          
            try
            {
                json = JSON.parse(body);
                console.log(json);
            }
            catch(err)
            {
                print(err);
                return;
            }
          
            if(json.status == 404)
            {
                twitchApiCallback(undefined);
            }
            else
            {
                twitchApiCallback(json);
            }
        }).on('error', (err) => {
            print(err);
        });
    }); 
}

Your headers are wrong.

        headers: {
            'cliend_id': config.twitchClientID,
            'client_secret': config.twitchClientSecret,
            'token_type': 'bearer'
        },

The Twitch Clietn Secret is NOT to be used in this way

You need to use the ClientID and Secret to generate an App Access Token, and use that

My original header only had the client ID and it was working. I was curious why all of a sudden I get that response

I ran this but when I do a console log nothing is returned

function twitchApiPost()
{
    let options = {
        hostname: 'id.twitch.tv',
        path: `/oauth2/token?client_id=${config.twitchClientID}&client_secret=${config.twitchClientSecret}&grant_type=client_credentials`,
        method: 'POST'
    }
    
    https.request(options, (res) => {
        let body = '';
      
        res.on('data', (chunk) => {
            body += chunk;
            console.log(body);
        });
      
        res.on('end', () => {
            let json;
          
            try
            {
                json = JSON.parse(body);
                console.log(json);
            }
            catch(err)
            {
                console.log(err);
                return;
            }
          
            if(json.status === 404)
            {
                console.log('404');
            }
        })
    });
}

Which console.log?

Also you may want to look at node-fetch or got to smooth talking to API’s rather than raw https

I haven’t used got at all but I found some request methods on using it but I am getting

(node:6489) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_URL]: Invalid URL: id.twitch.tv/oauth2/token?client_id=config.twitchClientID&client_secret=twitchClientSecret&grant_type=client_credentials

My code snippet for the got request

let url = `id.twitch.tv/oauth2/token?client_id=${config.twitchClientID}&client_secret=${config.twitchClientSecret}&grant_type=client_credentials`;
    got({
        url,
        method: 'POST',
        headers: {
            'Client-ID': config.twitchClientID,
            'Client-Secret': config.twitchClientSecret
        },
        responseType: 'json'
    })
    .then(res =>
    {
        console.log('Got', res.body.data[0]);
        if(res.body.pagination)
        {
            fetch(res.body.pagination.cursor);
        }
    });

Please do ignore the post above this because I got it using node-fetch. Thanks for the help!

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