2014-10-22 63 views
1

我正在開發一個腳本,用於使用google-api-php-client評估視頻。當它執行時,Google API迴應我:{「data」,null}並且沒有其他事情發生。使用google-api-php-client的視頻速率(like/dislike/none)

所以,我不知道我的代碼是否是錯誤的。在這裏,有使用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>";  
    } 
} 

回答

0

documentation說:

如果成功,此方法返回一個HTTP響應204的代碼(沒有 內容)。

如果出現錯誤,庫將引發異常。由於您沒有看到任何錯誤,這意味着請求已成功並保存了評分。您可以通過轉到視頻頁面並查看突出顯示的豎起大拇指圖標來檢查。

相關問題