← Other topics

YouTube API and Laravel

Video Notes

In my series YouTube API & PHP I covered how to communicate with the YouTube API using plain PHP and the google-api-php-client package.

Building on that series, this video shows how to apply those same techniques in the context of a Laravel application.

Here’s an outline of the notes from the YouTube API & PHP series, which are referenced throughout this video:

And here is the relevant code from this video:

/routes/web.php:

<?php

use Illuminate\Support\Facades\Route;
Route::get('/', 'App\Http\Controllers\DemoController@index');
Route::get('/edit', 'App\Http\Controllers\DemoController@edit');

/app/Http/Controllers/DemoController.php during Example 1 when we were retrieving details for a video:

<?php

namespace App\Http\Controllers;

use Google\Client;
use Google\Service\YouTube;

class DemoController extends Controller
{
    public function index()
    {
        # Configs
        $apiKey = config('app.youtube_api_key');

        # Initialize YouTube API client
        $client = new Client();
        $client->setDeveloperKey($apiKey);
        $service = new YouTube($client);

        # Example query just to make sure we can connect to the API
        $response = $service->videos->listVideos('snippet', ['id' => 'fG08dcJ8xFE']);

        # Output the response to confirm it worked
        dump($response);
    }
}

/app/Http/Controllers/DemoController.php during Example 2 when we were doing an OAuth connection and then editing a video

<?php

namespace App\Http\Controllers;

use Google\Client;
use Google\Service\YouTube;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

class DemoController extends Controller
{
    public function index(Request $request)
    {
        # Determines where the API server redirects the user after the user completes the authorization flow
        # This value must exactly match one of the authorized redirect URIs for the OAuth 2.0 client, which you configured in your client’s API Console Credentials page.
        $redirectUrl = 'https://redirectmeto.com/https://demo.test';

        # Create an configure client
        $client = new Client();
        $client->setAuthConfig(base_path('youtube.json'));
        $client->setRedirectUri($redirectUrl);
        $client->addScope('https://www.googleapis.com/auth/youtube');

        # === SCENARIO 1: PREPARE FOR AUTHORIZATION ===
        if(!$request->has('code') && !Session::has('google_oauth_token')) {
            Session::put('code_verifier', $client->getOAuth2Service()->generateCodeVerifier());
            # Get the URL to Google’s OAuth server to initiate the authentication and authorization process
            $authUrl = $client->createAuthUrl();

            $connected = false;
        }

        # === SCENARIO 2: COMPLETE AUTHORIZATION ===
        # If we have an authorization code, handle callback from Google to get and store access token
        if ($request->has('code')) {
            # Exchange the authorization code for an access token
            $token = $client->fetchAccessTokenWithAuthCode($request->input('code'), Session::get('code_verifier'));
            $client->setAccessToken($token);
            Session::put('google_oauth_token', $token);
            return redirect($redirectUrl);
        }

        # === SCENARIO 3: ALREADY AUTHORIZED ===
        # If we’ve previously been authorized, we’ll have an access token in the session
        if (Session::has('google_oauth_token')) {
            $client->setAccessToken(Session::get('google_oauth_token'));
            if ($client->isAccessTokenExpired()) {
                Session::forget('google_oauth_token');
                $connected = false;
            }
            $connected = true;
        }

        # === SCENARIO 4: TERMINATE AUTHORIZATION ===
        if(isset($_GET['disconnect'])) {
            Session::forget('google_oauth_token');
            Session::forget('code_verifier');
            return redirect($redirectUrl);
        }

        return view('demo')->with(['connected' => $connected, 'authUrl' => $authUrl ?? null]);
    }

    public function edit()
    {
        # Edit details
        $videoId = '2hDQp6M42hg'; # Must be a video that belongs to the currently auth’d user
        $newTitle = 'New Laravel application with Herd and DBngin';

        # Set up client and service
        $client = new Client();
        $service = new YouTube($client);

        # Authorize client
        # This assumes the auth process has already happened via the code
        # available here: https://codewithsusan.com/notes/youtube-api-php-oauth-connection#the-code
        if (Session::has('google_oauth_token')) {
            $client->setAccessToken(Session::get('google_oauth_token'));
        } else {
            # If not authorized, redirect back to index
            return redirect('/');
        }

        # Get the existing snippet details for this video pre-edit
        $response = $service->videos->listVideos(
            'snippet',
            ['id' => $videoId]
        );
        $video = $response[0];
        $snippet = $video->snippet;

        # Output the snippet details before the edits
        dump($snippet);

        # Set the edits
        $snippet->setTitle($newTitle);

        # Set the snippet
        $video->setSnippet($snippet);

        # Do the update
        $response = $service->videos->update('snippet', $video);
        dump($response->snippet);
    }
}

/resources/views/demo.blade.php:

<h1>Demo</h1>
<p>
    <strong>Status:</strong>
    @if ($connected)
        Authorized. <a href='?disconnect'>Disconnect</a>
    @else
        Not authorized.
        <a href='{{ $authUrl }}'>Authorize with YouTube...</a>
    @endif
</p>

If this info helped you out you can say thanks and support future content by clicking my Amazon affiliate link: https://amzn.to/3UtgnYk. If you make a purchase on Amazon within 24 hours of clicking the link I may receive a micro-commission and it costs you nothing extra. Any income from these sales goes directly to supporting me in making new videos and guides. Thank you for your support!

← Other topics