15

I am receiving an error message when trying to execute youtube analytics API.

A service error occurred: Error calling GET https://www.googleapis.com/youtube/analytics/v1/reports?ids=channel%3D%3DUCaayLD9i5x4MmIoVZxXSv_g&start-date=2016-08-01&end-date=2016-08-07&metrics=views&dimensions=7DayTotals: (403) Forbidden

Here is my code:

require_once __DIR__.'\google-api-php-client-1-master\src\Google\autoload.php';
require_once __DIR__.'\google-api-php-client-1-master\src\Google\Client.php';
require_once __DIR__.'\google-api-php-client-1-master\src\Google\Service\YouTube.php';
session_start();
$key = file_get_contents('mykey.json');
$OAUTH2_CLIENT_ID = 'xx-xx-xx';
$OAUTH2_CLIENT_SECRET = 'xxx';
$scope = 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 = new Google_Client();
    $client->setClientId($OAUTH2_CLIENT_ID);
    $client->setClientSecret($OAUTH2_CLIENT_SECRET);
    $client->setAccessType('offline');
    $client->setAccessToken($key);
    $client->setScopes($scope);
      if ($client->getAccessToken()) {        
    //Check to see if our access token has expired. If so, get a new one and save it to file for future use.
        if($client->isAccessTokenExpired()) {
            //refresh your access token if it's expired
            $newToken = json_decode($client->getAccessToken());
            $client->refreshToken($newToken->refresh_token);
            file_put_contents('mykey.json', $client->getAccessToken());
        }

        $analytics = new Google_Service_YouTubeAnalytics($client);
            $channel_url = 'UCaayLD9i5x4MmIoVZxXSv_g';
            $ids = 'channel==' . $channel_url . '';
            $end_date = '2016-08-07';
            $start_date = '2016-08-01';
            $optparams = array(
            'dimensions' => '7DayTotals',
            );
            $metric = 'views';
            $api = $analytics->reports->query($ids, $start_date, $end_date, $metric, $optparams);
            echo '<pre>';print_r($api);die;
  }

I have already enable 'youtube analytic API' and I get the access_token from here

What is wrong with my code ,Or do we need to do some more stuff to get rid from this?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Rahul
  • 440
  • 7
  • 24
  • Can anyone please suggest me how to fix this issue. – Rahul Sep 15 '16 at 09:23
  • Hey @Rahul, where are you mentioning your video ID of which you want to fetch analytics, in your code? It is missing in your code. – Tony Montana Sep 15 '16 at 13:43
  • I don't think it required video ID, I just want to get 30Day or 7 days Totals views of a specific channel. – Rahul Sep 15 '16 at 14:27
  • I don't think, it will work. How would you get a count of channel? You'll get count of video. – Tony Montana Sep 15 '16 at 15:04
  • I have also mentioned video ID now but still getting same issue. I'm using the same code [here](http://stackoverflow.com/questions/36883268/how-to-fetch-share-count-of-youtube-video-using-youtube-analytics-api-v3/38975597#38975597) I think this is the permission issue. I have already set up the client_id and client_secret and I have also enabled youtube analytic API from [google console](https://console.developers.google.com) – Rahul Sep 17 '16 at 08:10
  • When you run in youtube `API Explorer`, what do you get? Do you get success or the same `403 Forbidden` error? – Tony Montana Sep 19 '16 at 12:26
  • I am getting same 403 error. check the screen [here](http://screencast.com/t/WddITFNO) – Rahul Sep 19 '16 at 12:56
  • 2
    This is something related to permission. – Tony Montana Sep 19 '16 at 13:00
  • yes,Do you have any idea regarding this,or do we need some special account to use analytic API,or what. – Rahul Sep 19 '16 at 13:03
  • No need to have special account, let me check. – Tony Montana Sep 19 '16 at 13:10
  • ok, please.can I send you the credentials I am using for analytics so that you can check the setting for my account. if so please provide me your email-Id. – Rahul Sep 19 '16 at 13:14
  • No need to. Let me check at my end first, then I'll let you know. – Tony Montana Sep 19 '16 at 13:17
  • 1
    Do you have any youtube channel of yours? Use that channel id instead, and try to execute in `API Explorer` – Tony Montana Sep 19 '16 at 14:02
  • yes,I just check and API Explorer is working for my channel ID. So what about other channel,I want to get top 10 channels on the basis of views from last 7 days or 30 days. – Rahul Sep 19 '16 at 14:08
  • @Rahul Can you try `$channel_url = 'mine'` and see, my guess is the channel id you are using is not your own one. – Saumini Navaratnam Sep 19 '16 at 14:19
  • @Saumini where to use $channel_url = 'mine' ? in API Explorer and how? FYI: Now I'm getting response if I am using my channel Id instead of random one. but I want to get last 7days total views for some specific channel. – Rahul Sep 19 '16 at 14:25
  • @Rahul, will need to check. I don't know, how to, but will check. – Tony Montana Sep 19 '16 at 14:26
  • @Rahul in API explorer for `ids` parameter you mention `channel==mine`. I don't think you can retrieve other channels analytics. It's my guess – Saumini Navaratnam Sep 19 '16 at 14:30
  • @Rahul `channel==CHANNEL_ID – Set CHANNEL_ID to the unique channel ID of the channel for which you are retrieving data. The user authorizing the request must be the owner of the channel.` From the documentation. https://developers.google.com/youtube/analytics/v1/channel_reports – Saumini Navaratnam Sep 19 '16 at 14:32
  • it's the same as mentioned by @tony. I want to get other channel analytics,this is my problem – Rahul Sep 19 '16 at 14:33
  • @Rahul Sorry other than you have to be owner of the channel, no other way I see currently. Hope someone will help you. – Saumini Navaratnam Sep 19 '16 at 14:52
  • I have opened a separate question for this,If you have anything on your mind please reply on the same. [here](http://stackoverflow.com/questions/39588514/get-last-7days-30-days-views-subscriber-count-for-specific-channels) – Rahul Sep 20 '16 at 10:34
  • Guys I have found a site that doing the same thing as I want,Meant authorizing the request for any random channels [here](http://socialblade.com/) is the link.You can search for any channel and you will get the past day subscriber/view counts. I'm still struggling to get this done. – Rahul Sep 20 '16 at 13:08
  • Hey @Rahul, using `YouTube Data API v3`, you can achieve this. Right now, I am able to fetch total count of views, subscriber, total videos, etc of any channel. Now, I don't know, how to achieve the same for a specific period of time. – Tony Montana Sep 21 '16 at 14:46
  • @tony I can also able to get views,subscriber etc for any channel using YouTube data-API. But I want to use analytic to get view,subscriber for specific period of time like 7days or 30 days etc. there is a site that's doing the same thing,don't know how. [here](socialblade.com) – Rahul Sep 21 '16 at 15:02
  • @Rahul make sure you have added scope while taking permission from user. – Ankit vadariya Sep 25 '16 at 14:20

1 Answers1

2

Looking at your code, it seems that you are generating a new access token at the point you save it to file (invalidating the first one).

Try :

  if($client->isAccessTokenExpired()) {
        //refresh your access token if it's expired
        $newToken = json_decode($client->getAccessToken());
        $client->refreshToken($newToken->refresh_token);
        file_put_contents('mykey.json', $newToken);
    }
locksem
  • 315
  • 3
  • 16