title: Google cloudprint with google-php-api-client Oauth2 and curl returns 403 error while trying to cronjob
body: <p>I first used the following script to show all available printers (and I then obtained the refresh token from this script):</p>
  
  <pre><code>&lt;?php
  
  require_once 'XXX/Google_Client.php';
  
  session_start();
  
  
  
  $client = new Google_Client();
  $client-&gt;setApplicationName('XXX');
  $client-&gt;setScopes("https://www.googleapis.com/auth/cloudprint");
  $client-&gt;setAccessType('offline');
  
  $client-&gt;setClientId('XXX');
  $client-&gt;setClientSecret('XXX');
   $client-&gt;setRedirectUri('XXX');
  
  if (isset($_GET['code'])) {
  
    $client-&gt;authenticate();
    $_SESSION['token'] = $client-&gt;getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  }
  
  if (isset($_SESSION['token'])) {
   $client-&gt;setAccessToken($_SESSION['token']);
  }
  
  if (isset($_REQUEST['logout'])) {
    unset($_SESSION['token']);
    $client-&gt;revokeToken();
  }
  
  if ($client-&gt;getAccessToken()) {
  
    $_SESSION['token'] = $client-&gt;getAccessToken();
  
   $tokens = json_decode($_SESSION['token'], true);
   $client-&gt;refreshToken($tokens['refresh_token']);
   $tokens = json_decode($client-&gt;getAccessToken(), true);
  
  
  
   searchPrinters($tokens['access_token']);
  
  } else {
    $auth = $client-&gt;createAuthUrl();
  }
  
  if (isset($auth)) {
      print "&lt;a class=login href='$auth'&gt;Connect Me!&lt;/a&gt;";
    } else {
      print "&lt;a class=logout href='?logout'&gt;Logout&lt;/a&gt;";
  }
  
  
  function processRequest($url, $postFields, $referer,$access_token) {  
      $ret = "";
      $ch = curl_init(); 
  
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_USERAGENT, "");
      if(!is_null($postFields)) {
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS,
               $postFields);
          // http_build_query() will properly escape the fields and
          //  build a query string.
      }
  
      if(strlen($_SESSION['token']) &gt; 0) {
          $headers = array(
          "Authorization: OAuth " . $access_token,
          //"GData-Version: 3.0",
          "X-CloudPrint-Proxy", "XXX"
          );
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      }
  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_REFERER, $referer);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);    
      //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  
  
  
      $ret = curl_exec ($ch);
  
      curl_close ($ch); 
  
      return $ret;
  }
  
  
  
  
  function searchPrinters($access_token)
  {
      $url = "https://www.google.com/cloudprint/search?output=json";
  
      $post = array(
      );
  
      $ret = processRequest($url, $post, "",$access_token);
  
      echo $ret;
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>This script works fine to manually log in, but it doesn't work for cronjob.</p>
  
  <p>I hardcoded the refresh token in the following script:</p>
  
  <pre><code>&lt;?php
  
  
  require_once 'XXX/Google_Client.php';
  
  
  session_start();
  
  
  $refresh_token = 'XXX';
  
  $client = new Google_Client();
  $client-&gt;setApplicationName('XXX');
  $client-&gt;setScopes("https://www.googleapis.com/auth/cloudprint");
  $client-&gt;setAccessType('offline');
  
  $client-&gt;setClientId('XXX');
  $client-&gt;setClientSecret('XXX');
  $client-&gt;setRedirectUri('XXX');
  
  $client-&gt;refreshToken($refresh_token);
  
  
  $tokens = json_decode($client-&gt;getAccessToken(), true);
  
  
  searchPrinters($tokens['access_token']);
  
  function processRequest($url, $postFields, $referer,$access_token) {  
      $ret = "";
      $ch = curl_init(); 
  
      curl_setopt($ch, CURLOPT_URL,$url);
      curl_setopt($ch, CURLOPT_USERAGENT, "");
      if(!is_null($postFields)) {
          curl_setopt($ch, CURLOPT_POST, 1);
          curl_setopt($ch, CURLOPT_POSTFIELDS,
               $postFields);
          // http_build_query() will properly escape the fields and
          //  build a query string.
      }
  
      if(strlen($_SESSION['token']) &gt; 0) {
          $headers = array(
          "Authorization: OAuth " . $access_token,
          //"GData-Version: 3.0",
          "X-CloudPrint-Proxy", "XXX"
          );
          curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
      }
  
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
      curl_setopt($ch, CURLOPT_REFERER, $referer);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,  2);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);    
      //curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  
  
  
      $ret = curl_exec ($ch);
  
      curl_close ($ch); 
  
      return $ret;
  }
  
  
  
  
  function searchPrinters($access_token)
  {
      $url = "https://www.google.com/cloudprint/search?output=json";
  
      $post = array(
      );
  
      $ret = processRequest($url, $post, "",$access_token);
  
      echo $ret;
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>I thought once you obtained the refresh token you can always use it to regenerate access token and you only need to do </p>
  
  <pre><code>$client-&gt;refreshToken($refresh_token);
  
  $tokens = json_decode($client-&gt;getAccessToken(), true);
  </code></pre>
  
  <p>to get authorized. This could then be used in a cronjob to automatically do a printing job at a set time. But now I get a 403 error... Can someone explain to me what I'm doing wrong?</p>