我首先使用下面的脚本来显示所有可用的打印机(然后我从这个脚本中获得了刷新令牌):
<?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;
}
?>
该脚本可以很好地手动登录,但不适用于 cronjob。
我在以下脚本中对刷新令牌进行了硬编码:
<?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;
}
?>
我认为一旦您获得了刷新令牌,您就可以随时使用它来重新生成访问令牌,您只需要这样做
$client->refreshToken($refresh_token);
$tokens = json_decode($client->getAccessToken(), true);
获得授权。然后可以在 cronjob 中使用它在设定的时间自动执行打印作业。但是现在我收到 403 错误...有人可以向我解释我做错了什么吗?