Update:
Thanks to Ibrahim I now think am closer to getting this to work.
Using the example provided here (https://developers.google.com/youtube/partner/docs/v1/contentOwners/list), I get another Forbidden error:
Error calling GET googleapis..../youtube/partner/v1/contentOwners?fetchMine=true: (403) Forbidden
...
When trying this out from the API Explorer on the link below, I was able to fetch the YouTube CMS Account details. https://developers.google.com/youtube/partner/docs/v1/contentOwners/list
Also note that the "Developer Account" is linked to our "Youtube CMS Account" - and is a YouTube Partner.
Hoping someone could help out finding out what's causing this (403) Forbidden error I'm getting when trying to fetch Channels from a YouTube CMS account.
Here are some details and what I have done so far:
OS: Linux, Language: PHP 5
- Created a Project (@console.developers.google.com)
- Enabled YouTube Data API V3, YouTube Analytics and FreeBase API
- Created a Service Account Credentials for OAuth2
- Got the google-api-php-client and installed into the server
- Created a test script for Authenticating a Service Account (See Below for the Full PHP Code)
Getting results so far with these tests trying to fetch Channels:
Test 1) When I set the parameter: mine
to true
, it doesn't return an error, the channel it returns does not exist when I go to /youtube.com/channel/{channelID_returned}
Test 2) When I set the parameter: forUsername
to SomeYoutubeUsername
, it returns the channel of the User and confirmed that the channel id returned is correct and working.
Test 3) This is what I'm really trying to accomplish, I've set managedByMe
to true
and have set onBehalfOfContentOwner
to CMSContentOwner
, it then returns (403) forbidden error:
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling .... etc.etc (403) Forbidden
Just a note: I tried fetching Channels from a CMS Account using "Google oAuth Web Application" flow, and it worked.
Full Code:
<?php
ini_set('display_errors',1);
// Initialize...
$key_Path = '...omitted...';
set_include_path( '...omitted...' );
require_once 'Google/Client.php';
require_once 'Google/Service/YouTube.php';
// Setup Credentials
$client_id = '...omitted...';
$service_account_name = ''...omitted...';
$key_file_location = $key_Path.''...omitted....p12';
$client = new Google_Client();
$client->setApplicationName("'...omitted...");
$youtube = new Google_Service_YouTube($client);
/************************************************
If we have an access token, we can carry on.
Otherwise, we'll get one with the help of an
assertion credential. In other examples the list
of scopes was managed by the Client, but here
we have to list them manually. We also supply
the service account
************************************************/
$service_token = get_Configuration( 'GOOGLE_API' , 'TOKEN' );
//$client->revokeToken( $service_token );
if ( !empty($service_token) ) {
$client->setAccessToken($service_token);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
$service_account_name,
array(
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtubepartner',
'https://www.googleapis.com/auth/youtubepartner-channel-audit'
),
$key
);
$cred->sub = '...ommited...';
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($cred);
}
/************************************************
We're just going to make the same call as in the
simple query as an example.
************************************************/
$service_token = $client->getAccessToken();
$params = array( //'mine' => true,
//'forUsername' => ''...omitted...',
'managedByMe' => true,
'onBehalfOfContentOwner' => ''...omitted...' // CMS Account User ID
);
$channels = $youtube->channels->listChannels( 'contentDetails' , $params );
print_r( $channels );
//$Channels = $youtube->Channels->list('contentDetails','{mine : true}');
?>