php - Setting up Google Reporting API to work in cronjob -


i've managed set php script grabs data our google analytics account , can run great in browser i'm struggling figure out how allow work inside of cronjob script instead.

here's code:

<?php  // load google api php client library. require_once dirname(__file__) . '/analytics/google-api-php-client-2.2.0/vendor/autoload.php';  session_start();  $client = new google_client(); $client->setauthconfig(dirname(__file__) . '/analytics/client_secrets.json'); $client->setaccesstype("offline"); //offline access  $client->addscope(google_service_analytics::analytics_readonly);  if (isset($_session['access_token']) && $_session['access_token']) {      print "<br />start: ".date("h:i:s")."<br />";      // set access token on client.     $client->setaccesstoken($_session['access_token']);     $sessionid = $_session['access_token']['access_token'];      // create authorized analytics service object.     $analytics = new google_service_analyticsreporting($client);      $view_id = urlencode("ga:xxxxxx");     $start_date = "yesterday"; //can words or dates in y-m-d     $end_date = "yesterday"; //can words or dates in y-m-d     $metrics = urlencode("ga:transactions");     $dimensions = urlencode("ga:transactionid,ga:date,ga:ordercouponcode");     $sort = "ga:date"; //for desc use -field e.g. -ga:date     $sampling = "higher_precision"; //https://developers.google.com/analytics/devguides/reporting/core/v3/reference#samplinglevel     $limit = "10000";     $filters = urlencode("ga:ordercouponcode!=(not set)");      $params = "ids=$view_id&start-date=$start_date&end-date=$end_date&metrics=$metrics&dimensions=$dimensions&filters=$filters&sort=$sort&samplinglevel=$sampling&max-results=$limit&access_token=$sessionid";      $data = file_get_contents("https://www.googleapis.com/analytics/v3/data/ga?$params");     $json_data = json_decode($data);      print "<pre>";     print_r($json_data);     print "</pre>";  } else {  // handle authorization flow server.     if (! isset($_get['code'])) {          $auth_url = $client->createauthurl();         header('location: ' . filter_var($auth_url, filter_sanitize_url));      } else {          $client->authenticate($_get['code']);         $_session['access_token'] = $client->getaccesstoken();         $redirect_uri = 'http://' . $_server['http_host'] . '/cronjobs/ga_coupon_codes.php';         header('location: ' . filter_var($redirect_uri, filter_sanitize_url));      }  }  print "<br />end: ".date("h:i:s")."<br />"; 

when cronjob runs, servers send me email has finished running. i'm expecting see both "start" , "end" times printed in response, json data, thing that's coming in "end" time, leading me believe it's not running through main bit of it.

i know main reason why it's not working due authentication bit. in cronjob cannot use header('location: url') can't figure out change in order make work.

when set account, created service account , can see within api console have service account key.

i've got oauth 2.0 client id (which in client screts json file). if run in browser i'm getting connection work, after hour runs out (where it's getting new access token).

i've googled around quite time , found adding "offline access" should i'm still using header() function. can't find examples me use understandable me , i'd working in cronjob.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -