Livcecoding.tv API enables you to embed your stream on your site and use various resources.
In this article, we will use the Laravel framework and LiveCoding auth library hosted at https://github.com/LiveCodingTVOfficial/livecoding-auth
PREPARE
To start using API, you need to register an app on https://www.livecoding.tv/developer/applications/
AUTHORIZATION
The first thing we have to do before using API methods is to authorize.
Authorization in livecoding.tv API works through standard oAuth protocol, so first we need to implement methods to get an access token with API credentials and later refresh them.
Assume that we have model object with credentials data:
We need credentials data
- Client ID
- Client secret
- Redirect URL
- Scopes.
Client id and Client Secret are generated after app registration while Scopes as defined in LiveCoding API Documentation as follows.
Scopes
- “read”: Read basic public profile information
- “read:viewer”: Play live streams and videos for you
- “read:user”: Read your personal information
- “read:channel”: Read private channel information
- “chat”: Access chat on your behalf
To use multiple scopes use like ‘read:user read:viewer read:channel chat’. The multiple scopes are used to grant multiple permissions to the app by user to access their profile information like in the picture below.
Next, we can build things up to be used to authorize the app.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private $LivecodingAuth; public function __construct() { $client_id = '<client_id>'; $client_secret = '<client_Secret>'; $redirect_url = '<redirect_url>'; $scope = '<scopes>'; try { $this->LivecodingAuth = new LivecodingAuth($client_id, $client_secret, $redirect_url, $scope); } catch(Exception $ex) { die($ex->getMessage()); } } |
Further, we can proceed to check authorization status
1 2 3 4 5 6 7 8 |
public function index() { if (!$this->LivecodingAuth->getIsAuthorized()) { $this->LivecodingAuth->getAuthLink(); } else { $this->LivecodingAuth->fetchData('<api_end_point>'); } } |
Here in if condition we check for authorization status –
$this->LivecodingAuth->getIsAuthorized(), if it’s not authorized then we can proceed to generate the authentication link using – $this->LivecodingAuth->getAuthLink().
$this->LivecodingAuth->getAuthLink() generates authorization link like
1 |
'https://www.livecoding.tv/o/authorize/?scope=$scope&state=57d597c3b0703&redirect_uri=$redirect_uri&response_type=code&client_id=$client_id'. |
After successful authorization Livecoding.tv redirects to our redirect URL
1 |
<redirect_url>/?state=57df5425a5c80&code=<authorization_code> |
Now if $this->LivecodingAuth->getIsAuthorized() return true then else condition execute where we can perform our API calls.
API calls
After the successful authorization, we can perform API calls to endpoints mentioned here –
https://www.livecoding.tv/developer/documentation/#!/api
API call can be performed to end points using $this->LivecodingAuth->fetchData(‘<api_end_point>’)
Example
A sample API call to an endpoint can be like $this->LivecodingAuth->fetchData(‘user/’) to get loggedin user data similarly $this->LivecodingAuth->fetchData(‘videos/’) will return a json array consisting of all the videos from LiveCoding.tv.
Each API call returns JSON array of information. A sample API call returns following JSON array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
[ { "Url": "https://www.livecoding.tv/api/videos/051115-intelligent-snes-disassembler-python/", "slug": "051115-intelligent-snes-disassembler-python", "user": "https://www.livecoding.tv/api/users/andreaorru/", "title": "051115 Intelligent SNES disassembler [Python]", "description": "I'm building an intelligent disassembler for Super Nintendo ROMs.\r\nIt follows the execution flow of the program and tries to discover all the code in the ROM.\r\n\r\nThe goal is to extract all the info useful to statically recompile the games to port them on other platforms (i.e. Nintendo DS).", "coding_category": "Python", "difficulty": "expert", "language": "English", "product_type": "other", "creation_time": "2015-11-06T04:17:59.196863Z", "duration": 4100, "region": "eu-london", "viewers_overall": 16, … }, ... ] |
Auth Tokens
To save the auth tokens two type of storage is used session and text or file. To access tokens following variables can be used
- Access Token: $_SESSION[‘access_token’] or file_get_contents(‘access_token’)
- Refresh Token: $_SESSION[‘refresh_token’] or file_get_contents(‘refresh_token’)
- Expires In: $_SESSION[‘expires_in’] or file_get_contents(‘expires_in’)
These are all handled by the library by itself.
API Documentation
LiveCoding API Documentation can be accessed here.
Have any questions? Don’t forget to comment below and let us know.