2

I first used the following script to show all available printers (and I then obtained the refresh token from this 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() will properly escape the fields and
        //  build a 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 to manually log in, but it doesn't work for cronjob.

I hardcoded the refresh token in the 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() will properly escape the fields and
        //  build a 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 you obtained the refresh token you can always use it to regenerate access token and you only need to do

$client->refreshToken($refresh_token);

$tokens = json_decode($client->getAccessToken(), true);

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?

  • yeah, I was trying to do the same thing, but the token is different each time.... so..... I am thinking other alt, any ideas? – aahhaa Apr 28 '16 at 18:39

0 Answers0