我正在开发一个脚本,用于使用谷歌api php客户端对视频进行评级。当它执行时,Google API会回复我:{"data",null}而不会发生其他任何事情。
所以,我不知道我的代码是不是错了。这里有一个使用Google_Service_Youtube对视频进行评分的例子:
// SCRIPT FOR RATING A YOUTUBE VIDEO
// Trying to rate for this video: https://www.youtube.com/watch?v=ZE8ODPL2VPI
require_once realpath(dirname(__FILE__) . '/../autoload.php');
// API GOOGLE CLIENT PARAMS
$client_id = 'SET_CLIENT_ID_GOOGLE_API';
$client_secret = 'SET_CLIENT_SECRET_GOOGLE_API';
$redirect_uri = 'SET_REDIRECT_URI_GOOGLE_API';
// ID VIDEO FOR RATING
$id_video = 'ZE8ODPL2VPI';
$rating = 'like'; // Acceptable values: dislike, like, none
// LOCAL SCRIPT PARAMS
$is_auth = false;
// REQUEST (POST|GET) PARAMS
$idvideo = null;
$rating = null;
$code = null;
if(isset($_POST["idvideo"])) $idvideo = $_POST["idvideo"];
if(isset($_POST["rating"])) $rating = $_POST["rating"];
if(isset($_GET["code"])) $code = $_GET["code"];
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setScopes("https://www.googleapis.com/auth/youtube");
if(isset($_SESSION['youtube_data']) && !empty($_SESSION['youtube_data']))
$is_auth = true;
if($is_auth){
$token = $_SESSION['youtube_data'];
$client->setAccessToken($token);
if($idvideo != null && $rating != null){
$youtube = new Google_Service_YouTube($client);
$result = $youtube->videos->rate($idvideo,$rating);
echo $result;
}else{
echo '
<form action="rating_video.php" method="POST">
<input type="hidden" name="rating" value="'.$rating.'" />
<input type="hidden" name="idvideo" value="'.$id_video.'" />
<button type="submit">I like: '.$id_video.'</button>
</form>';
}
}else{
if($code != null){
$client->authenticate($code);
$_SESSION['youtube_data'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}else{
$authUrl = $client->createAuthUrl();
echo "<a href='$authUrl'>Sign in with Google </a>";
}
}
文档中写道:
如果成功,此方法将返回HTTP 204响应代码(否内容)。
如果出现错误,库将抛出异常。由于您没有看到任何错误,这意味着请求成功,评级已保存。您可以通过转到视频页面并看到竖起大拇指的图标高亮显示来检查这一点。