Download project archive

Code without authorization, we set only the ApiKey applications, it works fine

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(); } 

Code with authorization via Service account keys, does not work

 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(); } 

Several days of searching for errors did not produce results Creating a project in google console YouTube API Enabled List of all included APIs Filled content screen for OAuth just in case Added developer key Added service acaunt and got json file with authentication data Key list Changed security policy for google account Checked server time

Download project archive

  • What kind of error occurs? - Sergiks
  • {"error": "invalid_grant", "error_description": "Bad Request"} 400 - Vasily
  • Did you watch the OAuth 2.0 guide for Server to Server Applications ? - there is different than what you have implemented. - Sergiks
  • Yes, I saw this example, but I think it’s not quite suitable for me, in this example they are authorized as a cloud administrator to have access to all user data inside the cloud, I want to manage my YouTube channel and allow users of my site to upload Video on my channel - Vasily
  • The interaction takes place between the servers, your users do not have access there - so you can give full access. But read the link on the section β€œ Delegating Domain ”, Section 6, which you obviously missed. - Sergiks

1 answer 1

In the office. The β€œ Using OAuth 2.0 for Server to Server Applications ” documentation provides PHP examples β€” just what you need.

How to set application rights - described in the section Delegating domain-wide authority to the service account - there in p. 6 you will specify only access to youtube, youtube.upload.

  • $ user_to_impersonate = 'user@example.org'; $ credentials = new Google_Auth_AssertionCredentials ($ client_email, $ scopes, $ private_key, 'notasecret', // Default P12 password ' oauth.net/grant_type/jwt/1.0/bearer ', // Default grant type $ user_to_impersonate,); but there is no such class, but there is another Google_Auth_AssertionCredentials but when you call it, autoload does not occur, tried to uncomment it in autoload_classmap.php but the result is the same, can show how to connect it - Vasily
  • By the way the question is where to get the full client repository, the one that I had was assembled for parts github.com/google/google-api-php-client here is only part of the client without the vendor folder. Here I found the folder auth github.com/google/google-auth-library-php - Vasily
  • > without the vendor folder So there you need to run composer then. - Sergiks