最初に次のスクリプトを使用して、使用可能なすべてのプリンターを表示しました (そして、このスクリプトから更新トークンを取得しました)。
<?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 エラーが表示されます...誰かが私が間違っていることを説明してもらえますか?