【问题标题】:Video Rate (like/dislike/none) using google-api-php-client使用 google-api-php-client 的视频速率(喜欢/不喜欢/无)
【发布时间】:2014-12-17 20:27:07
【问题描述】:

我正在开发一个脚本,用于使用 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>";     
    }
}

【问题讨论】:

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


    【解决方案1】:

    documentation 说:

    如果成功,此方法返回 HTTP 204 响应代码(否 内容)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-16
      • 2012-01-14
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 2011-04-06
      • 2019-10-28
      • 1970-01-01
      相关资源
      最近更新 更多