2
votes

Here I am getting the next and previous url but when trying to access the next page with the url , I am getting error as

URL = https://www.googleapis.com/analytics/v3/data/ga?ids=ga:85914642&dimensions=ga:pagePath,ga:date&metrics=ga:pageviews,ga:uniquePageviews,ga:sessionDuration,ga:bounceRate,ga:exits&sort=-ga:pageViews&filters=ga:pageViews%3C%3D10&start-date=7daysAgo&end-date=today&start-index=11&max-results=10

Error: {"error":{"errors":[{"domain":"global","reason":"required","message":"Login Required","locationType":"header","location":"Authorization"}],"code":401,"message":"Login Required"}}

  public  function getPaginationInfo(&$results) {
        $client = new Google_Client();
        if( isset( $_SESSION['access_token'] ) ){
            $client->setAccessToken($_SESSION['access_token']);
        } 
        // Create an authorized analytics service object.
        $analytics = new Google_Service_Analytics($client);

        // Get the first view (profile) id for the authorized user.
        $profile = $this->getFirstProfileId($analytics);

        // Get the results from the Core Reporting API and print the results.
        $this->results = $this->getResults($analytics, $profile);
        var_dump( $this->results );

        $html = <<<HTML
        <pre>
        Items per page = {$this->results->getItemsPerPage()}
        Total results  = {$this->results->getTotalResults()}
        Previous Link  = {$this->results->getPreviousLink()}
        Next Link      = {$this->results->getNextLink()}
       </pre>
HTML; 

           print $html;
        }

I also want to know current page number , is there any function for that ??

1

1 Answers

0
votes

You need to add the accesss_token parameter: &access_token=ya29.AKDF..... You should be able to get it from your authorized client object:

$token = $client->getAccessToken();

And then append this token to your next page link.

$this->results->getPreviousLink() . "&access_token=" . $token

And then the link should be authenticated.