User data undefined?

I’m trying to get user ID from my account login name, but the data variable give ‘undefined’ back.

The authentication is working fine (got status 200) and the login name is correct. I don’t know what’s happening here.

Here’s my code:
require('dotenv').config()

const auth = require('./auth.json');

const axios = require('axios')

const clientID = (process.env.CLIENT_ID)

const getUserURL = `https://api.twitch.tv/helix/users?id=${process.env.USER_NAME}`

    axios({

        method: 'get',

        url: getUserURL,

        headers: {

            'Authorization': 'Bearer ' + auth[clientID].access_token,

            'Client-Id': clientID

        }})

        .then((res) => {

            console.log(res)

        })

A user name and user id are 2 separate things, so trying to use one for the other will not work.

The docs show there are 2 params you could use, https://dev.twitch.tv/docs/api/reference#get-users id or login, so if you’re wanting to use the user name rather than id, use the login param.

And if the access_token belongs to the user you want to look up, then you don’t have to specify a id or login as the users API will return the user for the authenticating token

I didn’t notice that. Thank you for your help! I can get data variables now.

Now I tried to fetch User ID but res.data.id is not working.

If #get-followed-streams can only be queried by user ID. What should I do to fetch ID?

res returns an object, that contains the axios response
res.data contains the actual body response

You’ll need to JSON decode it (unless axios did this for you)

var data_response = JSON.parse(res.data);

Then you’ll have the actual API response and can grab the users id from that

var user_id = data_response.data.[0].id;

Get Followed Streams needs the users token with the relevant scope attached to it, so call the users API with no query string params, and it’ll return the object that represents the user

That’ll return the user object for the user whom has logged in, then go and call the Get Followed Streams API, something like this, (not tested)

const getUserURL = `https://api.twitch.tv/helix/users`

    axios({

        method: 'get',

        url: getUserURL,

        headers: {

            'Authorization': 'Bearer ' + auth[clientID].access_token,

            'Client-Id': clientID

        }})

        .then((res) => {

            console.log(res)

            var data_response = JSON.parse(res.data);
            var user_id = data_response.data.[0].id;

            var urltocall = 'https://api.twitch.tv/helix/streams/followed?user_id=' + user_id;
        })

So

  • user logs in to your Tool with the relevant scope attached (user:read:follows)
  • call the Get Users API (with no query string param) to get the user data about the token
  • call the Get Followed Streams API with the userID from the response data

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