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.

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

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 

'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

<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.

[
{
            	"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

  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.

Dr. Michael J. Garbade

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.

View Comments

Recent Posts

Blockchain in Elections: A Leap Toward Transparent Democracy

In 2024 we're witnessing a critical point in democratic technology: the integration of blockchain and…

3 weeks ago

Win Big with Our Amazon Fire Max 11 & AirPods Pro Giveaway!

We’re thrilled to announce an exciting opportunity for you to win not one but two…

2 months ago

Unleashing Potential: How Education Ecosystem Transforms Learning into Real-World Success

Acquiring practical skills is crucial for career advancement and personal growth. Education Ecosystem stands out…

4 months ago

The Role of Artificial Intelligence in Modern Software Development

Artificial Intelligence (AI) has been making significant strides in various industries, and the software development…

7 months ago

Highest Stable Coin Yields – (W16 – 2024)

Another week to bring you the top yield platforms for three of the most prominent…

8 months ago

LEDU Token OTC Trading

If you hold a large volume of LEDU tokens above 1 million units and wish…

9 months ago