php - Multichannels on YouTube with YouTube Analytics API

608

I have a youtube account with 5 channels. With the YouTube Analytics API can I get data from the primary youtube channel. But the other channels I get always error message

forbidden

Also I am the owner from these all channels.

Here my sourcecode:

$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->setScopes( array( 
  'https://www.googleapis.com/auth/youtube.force-ssl', 
  'https://www.googleapis.com/auth/youtubepartner-channel-audit', 
  'https://www.googleapis.com/auth/youtube', 
  'https://www.googleapis.com/auth/youtube.readonly', 
  'https://www.googleapis.com/auth/yt-analytics.readonly', 
  'https://www.googleapis.com/auth/yt-analytics-monetary.readonly',
  'https://www.googleapis.com/auth/youtubepartner')
);

$client->setAccessType("offline");

$json_str = file_get_contents("access_token.json");
$json = json_decode($json_str, true);

if (isset($json['access_token']) && $json['access_token']) {
  $client->setAccessToken($json_str);
  $youtube = new Google_Service_YouTubeAnalytics($client);
  $report = $youtube->reports->query('channel==UC0xxxxxxxxxxx', '2017-04-01', '2017-04-30', 'views');
  echo json_encode($report);
} else {
  $redirect_uri = 'https://xxxxxxxxxx/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}

If i have in the query-Commandchannel==MINE or the YouTubeChannel-ID (UC0xxxxxxx) is work very well. But the otherChannel-ID from the same youtube account I get this error:

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message '{
"error": {
  "errors": [
    {
      "domain": "global",
      "reason": "forbidden",
      "message": "Forbidden"
    }
  ],
  "code": 403,
  "message": "Forbidden"
 }
}

How I can fix it?

People are also looking for solutions to the problem: php - How can I solve "Type error: Argument 2 passed to ... must be an instance of SymfonyComponentHttpFoundationFileUploadedFile"?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.