I’m trying to work through the Twitch.tv intermediate front-end project and have encountered a problem. I’ve gotten as far as being able to set up the AJAX function call and it is successful but does not return anything. To be a bit clearer on this confusing point, I have a success function and an error function set up in my AJAX call. When I call getStreams, the error function is not triggered, hence it is a ‘successful’ AJAX call but the server refuses to return the object I’m requesting. Here is the code for the AJAX function:
function getStreams(users) {
var streams = "streams/";
var streamUrls = getUrl(streams, users);
$.ajax({
url: streamUrls[0],
type: 'GET',
dataType: 'json',
success: function(data) {
var isStreaming = data.stream;
var game = data.stream.game;
var link = "https://www.twitch.tv/" + users[0];
if (isStreaming !== null) {
display for online with code below
displayUser(users[0]);
displayInfo(users[0], game, link);
console.log(data);
}
},
error: function(err) {
//display for error
console.log("error");
}
});
}
Note that streamsUrls is an array but to test it out it only has one item in it at the moment hence the hard-coded index of 0.
Upon trying to troubleshoot this, I get the following error in my console log: Object {error: “Bad Request”, message: “No client id specified”, status: 400}
We require you to send your application’s client_id with every request you make to ensure that your application is not rate limited. You should do so by sending the following HTTP header:
Client-ID: <client_id>
In situations where headers cannot be set, you can also specify a client ID as a querystring parameter: client_id=<client_id>"
I have looked at quite a few codepens of other campers and so far the only one I have noticed working ‘properly’ has a client_id so my question is: how do you get one? Sorry if that seems like such a basic question but that’s the one thing they don’t seem to mention in the API documentation.
Log in to twitch and scroll down to “Register your application”
Add a name for your project and set the Redirect URI to your CodePen project URL without the editors part (mine was http://codepen.io/ColtonBoston/pen/wGEzQg). Then click register. You should now see your Client ID.
This part could differ based on how you retrieve your JSON, but here’s how to edit yours if you used $.getJSON:
This should get it working again. If there’s a better way, please let me know. Also, remember to add the Client ID to your other twitch json calls if you have any (I had one for online users (/streams/) and another for offline users (/channels/)). I hope this helped!
If this works is going to be great. You have even clarify for me how/where to insert the client_id in the http request string (right after “?”), something that nobody did as far as I have read so far.
This will allow me to use vanilla JS (I don´t see the point of using libraries if not needed). Let´s see if I am lucky!
Just came back after taking time off from FCC and was checking over my old projects. Several were broken, including the Twitch TV app, so a big thank you for this information.
Thanks from here too. I was trying to figure out a way to send this as a query string parameter on the URL and couldn’t manage to get that to work, but this example works perfectly