How to get user id from user name (noob)

hi, new here using the twitch api, starting from an example in javascript i made it work and get data from the /kraken/users/ , /kraken/channels/ and /kraken/stream/ requests , using something like this :

var clientID = 'xxxxxxxxxxxxxxxxxxxxxxxxx';
var userID = 'yyyyyyy';
var twitch_user_api = new XMLHttpRequest();
twitch_user_api.open('GET', 'https://api.twitch.tv/kraken/users/' + userID + '?client_id=' + clientID, true);
twitch_user_api.setRequestHeader('Accept', 'application/vnd.twitchtv.v5+json');
twitch_user_api.onload = function() {
	var data = JSON.parse(twitch_user_api.responseText);
	if (data) {
		document.getElementById('twitch_1').innerHTML = 'stream bio  : ' + data.bio;
	} else {
		document.getElementById('twitch_1').innerHTML = '----';
	}
}
twitch_user_api.send(null);

now, i have this problem when i want to get the user id from the user name as described in https://dev.twitch.tv/docs/v5/guides/using-the-twitch-api/ , the code i have so far to test is : ( using the example from that link, later gonna replace that for a user using php and a form, clientID is the same value from the others working examples i have , and so far i understand i don’t need the Aouth there, right?)

var clientID = 'xxxxxxxxxxxxxxxxx';
var twitch_user_api = new XMLHttpRequest();
twitch_user_api.open('GET', 'https://api.twitch.tv/kraken/users?login=dallas,dallasnchains'+ '?client_id=' + clientID   , true );
twitch_user_api.setRequestHeader('Accept', 'application/vnd.twitchtv.v5+json');
twitch_user_api.onload = function() {
	var data = JSON.parse(twitch_user_api.responseText);
		if (data) {
		document.getElementById('twitch_1').innerHTML =  'user id  : ' + data.users[0]._id ;

	} else {
		document.getElementById('twitch_1').innerHTML = 'no user';
	}
};
twitch_user_api.send(null);

from what i understand of that json response i should use data.users[0]._id to get what i need (later when i have only 1 name is going to be 0 too? ), but nothing is displaying
any idea in the right direction to make this work? i would like to keep using only just js , not php or something else, thanks in advance!

data.users[0]._id should be correct. Should start by looking at what data contains to make sure you’re not getting some error json back.

uhm, like i said kinda noob in js ,but i would say that i am not connecting properly in first place then i don’t get the data variable with the response ?

and firefox shows me this :

fox

Change “?client_id=” to “&client_id=”

2 Likes

yes! that did the trick, thanks!

I probably ought to explain why. When using query-string parameters, the first must begin with “?” and any subsequent params should instead begin with “&”.

2 Likes

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