Код без авторизации, задаем только ApiKey приложения, работает нормально
require_once __DIR__ . '/php/Gapi/vendor/autoload.php';
set_include_path( __DIR__ . '/php/Gapi/src');
$client = new Google_Client();
$client->setDeveloperKey('AIzaSyAjqunVy9S_Jm8HMncSS0WZkkqBNBsebck');
$youtube = new Google_Service_YouTube($client);
try{
$listSearch = $youtube->search->listSearch('id,snippet',array(
'q' => 'Dogs',
'maxResults' => 30,
));
$html = '';
foreach($listSearch['items'] as $searchItem){
switch($searchItem['id']['kind']){
case 'youtube#video':
$html .= '<a target="_blank" href="http://www.youtube.com/watch?v='.$searchItem['id']['videoId'].'">'.$searchItem['snippet']['title'].'</a><br>';
break;
}
}
echo $html;
}
catch(Google_Service_Exception $e){
echo $e->getMessage().' '.$e->getCode();
}
catch(Google_Exception $e){
echo $e->getMessage().' '.$e->getCode();
}
Код с авторизации через Service account keys, не работает
require_once __DIR__ . '/php/Gapi/vendor/autoload.php';
set_include_path( __DIR__ . '/php/Gapi/src');
$client = new Google_Client();
$client->setAuthConfig( __DIR__ . '/pn-gapi-201d1056019b.json');
$client->setScopes(array(
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtube.upload',
));
$client->setApplicationName('an_gapi');
$client->setAccessType("offline");
$youtube = new Google_Service_YouTube($client);
try{
$listSearch = $youtube->search->listSearch('id,snippet',array(
'q' => 'Dogs',
'maxResults' => 30,
));
$html = '';
foreach($listSearch['items'] as $searchItem){
switch($searchItem['id']['kind']){
case 'youtube#video':
$html .= '<a target="_blank" href="http://www.youtube.com/watch?v='.$searchItem['id']['videoId'].'">'.$searchItem['snippet']['title'].'</a><br>';
break;
}
}
echo $html;
}
catch(Google_Service_Exception $e){
echo $e->getMessage().' '.$e->getCode();
}
catch(Google_Exception $e){
echo $e->getMessage().' '.$e->getCode();
}