Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting 'Forbidden error' when trying to execute youtube analytic API

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?

like image 500
Rahul Avatar asked Sep 15 '16 07:09

Rahul


People also ask

Why is my API request forbidden?

An HTTP 403 response code means that a client is forbidden from accessing a valid URL. The server understands the request, but it can't fulfill the request because of client-side issues. The caller isn't authorized to access an API that's using an API Gateway Lambda authorizer.


1 Answers

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);
    }
like image 157
locksem Avatar answered Sep 27 '22 18:09

locksem