0
votes

how can I replace OAuth2 access to Public API Access Key in Youtube API v3? Should I use setDeveloperKey function? If I do so as showed below it returns:

Access Not Configured. Please use Google Developers Console to activate the API for your project.

I have set my Public Access API in Google console so it should be okay.

When I try to access via OAuth it works perfectly.

CORRECTED CODE:
  // Call set_include_path() as needed to point to your client library.
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
session_start();

$API_KEY = 'AIzaSyB7HuVrtyiUV8ow1SegS6xxxxxxxxx';
$client = new Google_Client();
$client->setDeveloperKey($API_KEY);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
  FILTER_SANITIZE_URL);
$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
    // Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'id' => 'UUJQ2_kh7m7muj2xxxx',
    ));

    $htmlBody = '';

      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => => 'UUJQ2_kh7m7muj2xxxx',
        'maxResults' => 50
      ));

      $htmlBody .= "";
      foreach ($playlistItemsResponse['items'] as $playlistItem) {
       $title =  $playlistItem['snippet']['title'];
       $video_id = $playlistItem['snippet']['resourceId']['videoId'];
          $htmlBody .= "<a href='https://www.youtube.com/watch?v=".$video_id."'>";
          $htmlBody .= "<img src='http://img.youtube.com/vi/".$video_id."/1.jpg'>".$title."</a></br>";
      }
      $htmlBody .= '</ul>';
?>

<!doctype html>
<meta charset="utf-8">
<html>
  <head>
    <title>My Uploads</title>
  </head>
  <body>
    <?=$htmlBody?>
  </body>
</html>

ERROR ON GET:

"domain": "usageLimits", "reason": "accessNotConfigured", "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project." } ], "code": 403, "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."

1
Which request is given the error ? YouTube API version 3 is not using a developerKey. Add the api key as a parameter to your request, like: &key=AIzaSyBsRpXjl8oAymwFZ0CMxxxxxxxxxx - Als
And how can I replace the OAuth verification by API key solution? I was trying to change the example from google developers site. Can I use this solution with API key? I used exactly this (of course with my access key) - developers.google.com/youtube/v3/code_samples/… - Lukáš Ježek
OAuth 2 will also work for older api version requests as far as they are not obsolete. OAuth 2, is used to get an access_token, which is needed to access private YouTube data. Then for a GET request you can add the parameter &access_token=..... instead of &key=..... - Als
That example "Retrieve my uploads", that you refer to: Did that example worked before you made any change in the code ? - Als
Yes it does work fine if I use OAuth, but I want to enable my friends to see list of my uploaded videos (only public ones) and if I use OAuth they would have to log via my google account. Thats why I thought to use Public access Key but I cannot figure out how to modify the example I sent to use API key instead of OAuth. - Lukáš Ježek

1 Answers

0
votes

Changed your code. This works for me.

<?PHP
 // (1): Choose a valid channel_id
 $myChannelId = 'UUJQ2_kh7m7muj2xxxx';              // 'REPLACE_ME';
 // (2): api version 3
 $API_KEY = 'AIzaSyB7HuVrtyiUV8ow1SegS6xxxxxxxxx';  // 'REPLACE_ME';

 // (3): Library from: https://github.com/google/google-api-php-client
 // set_include_path();  // REPLACE_ME as needed to point to your client library.
 require_once 'Google/Client.php';
 require_once 'Google/Service/YouTube.php';

session_start();

$client = new Google_Client();
$client->setDeveloperKey($API_KEY);
//$client->setScopes('https://www.googleapis.com/auth/youtube');
//$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
//  FILTER_SANITIZE_URL);
//$client->setRedirectUri($redirect);

// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
    // Call the channels.list method to retrieve information about the
    // currently authenticated user's channel.
    $channelsResponse = $youtube->channels->listChannels('contentDetails', array(
      'id' => $myChannelId,
    ));

    $htmlBody = '';
    foreach ($channelsResponse['items'] as $channel) {
      // Extract the unique playlist ID that identifies the list of videos
      // uploaded to the channel, and then call the playlistItems.list method
      // to retrieve that list.
      $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];
      $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet', array(
        'playlistId' => $uploadsListId,
        'maxResults' => 50
      ));
      $htmlBody .= " ";

      foreach ($playlistItemsResponse['items'] as $playlistItem) {
       $title =  $playlistItem['snippet']['title'];
       $video_id = $playlistItem['snippet']['resourceId']['videoId'];
       $htmlBody .= "<a href='https://www.youtube.com/watch?v=".$video_id."'>";
       $htmlBody .= "<img src='http://img.youtube.com/vi/".$video_id."/1.jpg'>".$title."</a></br>";
      }
      $htmlBody .= '</ul>';
      }
?>

<!doctype html>
<meta charset="utf-8">
<html>
  <head>
    <title>My Uploads</title>
  </head>
  <body>
    <?PHP print $htmlBody?>
  </body>
</html>