Coding Community

How to use the Livecoding.tv API using PHP

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

  1. Client ID
  2. Client secret
  3. Redirect URL
  4. 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.

Further, we can proceed to check authorization status

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 

After successful authorization Livecoding.tv redirects to our redirect URL

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.

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

  1. Access Token: $_SESSION[‘access_token’] or file_get_contents(‘access_token’)
  2. Refresh Token: $_SESSION[‘refresh_token’] or file_get_contents(‘refresh_token’)
  3. 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.

Avatar
About author

I, Dr. Michael J. Garbade is the co-founder of the Education Ecosystem (aka LiveEdu), ex-Amazon, GE, Rebate Networks, Y-combinator. Python, Django, and DevOps Engineer. Serial Entrepreneur. Experienced in raising venture funding. I speak English and German as mother tongues. I have a Masters in Business Administration and Physics, and a Ph.D. in Venture Capital Financing. Currently, I am the Project Lead on the community project -Nationalcoronalvirus Hotline I write subject matter expert technical and business articles in leading blogs like Opensource.com, Dzone.com, Cybrary, Businessinsider, Entrepreneur.com, TechinAsia, Coindesk, and Cointelegraph. I am a frequent speaker and panelist at tech and blockchain conferences around the globe. I serve as a start-up mentor at Axel Springer Accelerator, NY Edtech Accelerator, Seedstars, and Learnlaunch Accelerator. I love hackathons and often serve as a technical judge on hackathon panels.