使用Google php api客户端Oauth2和curl的Google cloudprint在尝试cronjob时返回403错误
我首先使用以下脚本显示所有可用的打印机(然后从该脚本获得刷新令牌):使用Google php api客户端Oauth2和curl的Google cloudprint在尝试cronjob时返回403错误,cron,oauth-2.0,google-oauth,google-api-php-client,google-cloud-print,Cron,Oauth 2.0,Google Oauth,Google Api Php Client,Google Cloud Print,我首先使用以下脚本显示所有可用的打印机(然后从该脚本获得刷新令牌): 获得授权。然后可以在cronjob中使用它在设定的时间自动执行打印作业。但现在我得到一个403错误。。。有人能给我解释一下我做错了什么吗?是的,我试着做同样的事情,但每次的标记都不一样。。。。所以我在想其他的事情,有什么想法吗? <?php require_once 'XXX/Google_Client.php'; session_start(); $client = new Google_Client();
获得授权。然后可以在cronjob中使用它在设定的时间自动执行打印作业。但现在我得到一个403错误。。。有人能给我解释一下我做错了什么吗?是的,我试着做同样的事情,但每次的标记都不一样。。。。所以我在想其他的事情,有什么想法吗?
<?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;
}
?>
<?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);