Use Youtube Client V3 API

Created on 14 February 2023, almost 2 years ago

Problem/Motivation

I started using this module on the 8.x-1.x branch but it seems the porting of the functionality towards Youtube API V3 was interrupted.

Therefore, the module seems to be unable to perform API calls as a the old, deprecated gdata.youtube.com URL is still in use.

We can see this because the googleapis Youtube Client is loaded:

  public function get(String $source, $id) {
    $client = $this->getClientFactory($id);
    $youtube = new \Google_Service_YouTube($client); // <----- new V3 client is loaded

But it is not used and instead direct API calls are still used based on a user-provided $source URL (which seems to be the old gdata.youtube.com feed:

  /**
   * Request URL.
   *
   * @param string $url
   *   The URL to request.
   * @param string $page_token
   *   The page token for the request.
   *
   * @return array|null
   */
  public function requestUrl($url, $page_token = '') {
    try {
      $url .= '&maxResults=' . $this->getConfiguration('results_per_page');
      $url .= ($page_token != '') ? '&pageToken=' . $page_token : '';
      $api_request_result = \Drupal::httpClient()->get($url);
      $data = (string) $api_request_result->getBody();
      return json_decode($data, TRUE);
    }
    catch (RequestException $e) {
      \Drupal::messenger()->addError($this->t('Request Error: @message', ['@message' => $e->getMessage()]));
    }
  }

Steps to reproduce

Try to use the module: without the gdata URL it can't work because it won't ever use the youtube API V3.

Proposed resolution

Refactor the Fetcher in order to use the Youtube Client to make calls using the Youtube API V3.

Remaining tasks

Do it.

User interface changes

Instead of inputting a gdata URL, the user should input a Playlist ID. This could be the general, "uploads" playlist ID if the user wants to have all videos.

API changes

TBD

Data model changes

None

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇪🇸Spain idiaz.roncero Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @idiaz.roncero
  • @idiazroncero opened merge request.
  • 🇪🇸Spain idiaz.roncero Madrid

    The MR added solves the issue but is to be considered a starting point.

    The following remains TBD:

    - Recover the validateConfigurationForm method. This would mean some refactor on the Fetcher.
    - Handling API errors,
    - Find a better way to match Video and PlaylistItem resources.
    - Testing, testing, testing

Production build 0.71.5 2024