cron - Google cloudprint with google-php-api-client Oauth2 and curl returns 403 error while trying to cronjob -


i first used following script show available printers (and obtained refresh token script):

<?php  require_once 'xxx/google_client.php';  session_start();    $client = new google_client(); $client->setapplicationname('xxx'); $client->setscopes("https://www.googleapis.com/auth/cloudprint"); $client->setaccesstype('offline');  $client->setclientid('xxx'); $client->setclientsecret('xxx');  $client->setredirecturi('xxx');  if (isset($_get['code'])) {    $client->authenticate();   $_session['token'] = $client->getaccesstoken();   $redirect = 'http://' . $_server['http_host'] . $_server['php_self'];   header('location: ' . filter_var($redirect, filter_sanitize_url)); }  if (isset($_session['token'])) {  $client->setaccesstoken($_session['token']); }  if (isset($_request['logout'])) {   unset($_session['token']);   $client->revoketoken(); }  if ($client->getaccesstoken()) {    $_session['token'] = $client->getaccesstoken();   $tokens = json_decode($_session['token'], true);  $client->refreshtoken($tokens['refresh_token']);  $tokens = json_decode($client->getaccesstoken(), true);     searchprinters($tokens['access_token']);  } else {   $auth = $client->createauthurl(); }  if (isset($auth)) {     print "<a class=login href='$auth'>connect me!</a>";   } else {     print "<a class=logout href='?logout'>logout</a>"; }   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() escape fields ,         //  build query string.     }      if(strlen($_session['token']) > 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;  }  ?> 

this script works fine manually log in, doesn't work cronjob.

i hardcoded refresh token in following script:

<?php   require_once 'xxx/google_client.php';   session_start();   $refresh_token = 'xxx';  $client = new google_client(); $client->setapplicationname('xxx'); $client->setscopes("https://www.googleapis.com/auth/cloudprint"); $client->setaccesstype('offline');  $client->setclientid('xxx'); $client->setclientsecret('xxx'); $client->setredirecturi('xxx');  $client->refreshtoken($refresh_token);   $tokens = json_decode($client->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() escape fields ,         //  build query string.     }      if(strlen($_session['token']) > 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;  }  ?> 

i thought once obtained refresh token can use regenerate access token , need

$client->refreshtoken($refresh_token);  $tokens = json_decode($client->getaccesstoken(), true); 

to authorized. used in cronjob automatically printing job @ set time. 403 error... can explain me i'm doing wrong?


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -