1
votes

I have started a small android project and its last feature is watching twitch streams but I have run into a setback. I have red the twitch API and later found this post . My idea is the following:

-get the list of twitch streamers for a particular game.

-when an element of the list is clicked open the stream and play.

After reading those two sources I made a listfragment with arrayadapter and using volley I loaded the response from https ://api.twitch.tv/kraken/streams?game=(MyGame) . ex:

https://api.twitch.tv/kraken/streams?game=League%20of%20Legends

I get JSON reponse for the 25 top streamers and display them. Here is where I run in a problem. I don't know how to get the streams video URL.

According to my second link to get the different quiality stream videos I have to:

1) get token from http ://api.twitch.tv/api/channels/(CHANNELNAME)/access_token

The response recieved looks like this:

{ token: "{"user_id":null,"channel":"riotgames","expires":1408096127,"chansub":{"view_until":1924905600,"restricted_bitrates":[]},"private":{"allowed_to_view":true},"privileged":false}",
sig: "c81dfc25b90d44ea107cdeae5371df28185bd0dd",
mobile_restricted: false
}

this response has 3 values: token, sig and mobile_restricted.

2) using this http ://usher.twitch.tv/select/(CHANNELNAME).json?nauthsig=(SIG)&nauth=(TOKEN)&allow_source=true , I fill in the fields using the json token response

http://usher.twitch.tv/select/riotgames.json?nauthsig=c81dfc25b90d44ea107cdeae5371df28185bd0dd&nauth="{"user_id":null,"channel":"riotgames","expires":1408096127,"chansub":{"view_until":1924905600,"restricted_bitrates":[]},"private":{"allowed_to_view":true},"privileged":false}"&allow_source=true

, but the response I get back is always:

[]

Which to my understanding means that the stream is not live? But in the same time I can see the stream going.

My questions are: How do I get the live stream urls with the different qualities? What is wrong with the way I call them?

As a disclaimer: I'm extremely new to any sort of programming so I don't pretend to understand everything. I might have overlooked something or clearly missed it. Also it seems I can't post more than 2 links so I have added an additional space after http.

Edit: With the help of @rcxrdx I got the response I was looking for. What he pointed out was that I have to encode the token correctly. Here is an example of encoded url for recieving the String response with all the video qualities:

http://usher.twitch.tv/select/reckful.json?segment_preference=2&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22channel%22%3A%22reckful%22%2C%22expires%22%3A1408897471%2C%22chansub%22%3A%7B%22view_until%22%3A1924905600%2C%22restricted_bitrates%22%3A%5B%5D%7D%2C%22private%22%3A%7B%22allowed_to_view%22%3Atrue%7D%2C%22privileged%22%3Afalse%7D&allow_source=true&type=any&nauthsig=b0e19347528f3675612d4c5d1a030bfa8d9ffb13

Something I lost some time to is that when you recieve the access token if you are not logged in the user_id will return null. In most examples like the one from the blog post i have added up top, do not explicitly say that. So yeah, if someone wants to play a twitch video the hierarchy is as follows: get access_token, form the url following my example in the edit , recieve the STRING (it might say it's JSON but it isnt) response, get the video qualities from the response and use a VideoView to play them with the following lines:

String myStream=response;
Uri uri = Uri.parse(myStream);
videoView.setVideoURI(uri);
videoView.requestFocus();
videoView.start();
2

2 Answers

1
votes

You are doing it right, the only thing you are missing is that your token has to be URL-encoded, by the method of your choice.

If you want to make sure that you are doing the encoding step correctly, you can watch the request you send when connecting to a stream through your browser (with wireshark, or the Network tab on Chrome), and make sure you are sending the same token.

0
votes

Im not sure how much this will help you but i was doing work with the twitch API a few months ago I found it very hard but was able to get game searches working and to bring in streams

hope it helps here is the code that i used

//Twitch Streams //Ref: https://github.com/justintv/Twitch-API/blob/master/embedding.md

function getTwitch(title){
$.getJSON("https://api.twitch.tv/kraken/search/streams?q="+title+"&limit=20&type=top&callback=?", function (data) {
    var temp = "";

    $.each(data.streams.slice(0,5), function (index, item) {
        temp = temp + "<a target='frame1' href='http://www.twitch.tv/widgets/live_embed_player.swf?channel=" + item.channel.name + "'><button>"+item.channel.display_name+"</button></a><br />";
    });
    $("#StreamList").html(temp);
});
}

Here is a sample jsfiddle i build using the original api code that u my use as a jumping of point aswell but the code above is the final code I used

http://jsfiddle.net/F4tsM/10/