视频率(喜欢/不喜欢/无)使用google-api-php-client

时间:2014-10-22 11:39:04

标签: php youtube-api google-api-php-client

我正在使用google-api-php-client开发一个评级视频的脚本。执行后,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>";     
    }
}

1 个答案:

答案 0 :(得分:0)

documentation说:

  

如果成功,此方法返回HTTP 204响应代码(No.   含量)。

如果出现错误,库将抛出异常。由于您没有看到任何错误,这意味着请求成功并且保存了评级。您可以通过转到视频页面并查看竖起大拇指图标突出显示来查看此内容。

相关问题