2013-03-27 106 views
4

我正在嘗試在YouTube視頻上發佈評論...爲此,我使用YouTube api。下面是代碼:爲什麼這個CURL請求不起作用?

<?php 
$message="Just Some Comment..."; 
$developer_key="<!---visit demo for actual code---!>"; 
$access_token=$_GET['code']; 
if(!$access_token){ Header("Location: <!---visit demo for actual code---!>");} 
$video_id="I3LMKhu2-vo"; 
$message_xml='<?xml version="1.0" encoding="UTF-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom"  xmlns:yt="http://gdata.youtube.com/schemas/2007"> 
<content>' . $message . '</content> 
</entry>'; 
$url = "http://gdata.youtube.com/feeds/api/videos/" . $video_id . "/comments"; 
$header = array('Content-Type: application/atom+xml', 'Content-Length: ' . strlen($message_xml), 'Authorization: Bearer "' . $access_token . '"', 'GData-Version: 2', 'X-GData-Key: key=' . $developer_key); 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "$message_xml"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 
echo curl_error($ch); 
curl_close($ch); 
echo $access_token; 
?> 

如果我引用這是隱藏個人的東西,但你可以在votm.net78.net 看到演示所以我的問題是,爲什麼不評論出現在視頻,即使用戶已經發送授權令牌?請問我可以幫忙嗎?謝謝!

+0

'$ result'中有什麼?如果在'$ result = curl_exec($ ch);'後面加上'var_dump($ result);',你會看到什麼? – 2013-04-01 07:34:35

回答

2

我認爲缺少的主要是你的代碼是你必須通過調用令牌服務(請參閱我的代碼中的第2步)使用授權代碼來獲取真正的access_token。這意味着您將共有兩個捲曲請求。有關詳細信息,看看在文檔:https://developers.google.com/accounts/docs/OAuth2WebServer?hl=de#handlingtheresponse

此外,你需要創建(除非已經完成)項目上https://code.google.com/apis/console/爲授權API訪問創建Client IDClient secret。除了developer key之外,這是必需的。

隨着一些額外的錯誤檢查,我製作了以下代碼併成功地進行了測試。我認爲劇本是通過URL

http://localhost/youtube.php 

訪問:

<?php 

$developer_key='<!---hidden---!>'; 
$client_id=  '<!---hidden---!>'; 
$client_secret='<!---hidden---!>'; 

// error checking; user might have denied access 
if (isset($_GET['error'])) { 
    if ($_GET['error'] == 'access_denied') { 
     echo('You have denied access. Click <a href="'. $_SERVER["SCRIPT_NAME"] .'">here</a> to retry&hellip;'); 
    } else { 
     echo("An error has occurred: ". $_GET['error']); 
    } 
    exit; 
} 

// Step 1: redirect to google account login if necessary 
if(!isset($_GET['code']) || $_GET['code'] === '') { 
    Header('Location: https://accounts.google.com/o/oauth2/auth?client_id='. $client_id . 
      '&redirect_uri=http%3A%2F%2Flocalhost%2Fyoutube.php' . 
      '&scope=https://gdata.youtube.com&response_type=code&access_type=offline', 
     true, 307); 
    exit; 
} 
$authorization_code= $_GET['code']; 

// Step 2: use authorization code to get access token 
$url = "https://accounts.google.com/o/oauth2/token"; 
$message_post= 'code='. $authorization_code . 
     '&client_id='. $client_id . 
     '&client_secret='. $client_secret . 
     '&redirect_uri=http://localhost/youtube.php' . 
     '&grant_type=authorization_code'; 

$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_post); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 

if ($cur_error= curl_error($ch)) { 
    echo($cur_error); 
    curl_close($ch); 
    exit; 
} 
curl_close($ch); 

$jsonArray= json_decode($result, true); 

if ($jsonArray === null) { 
    echo("Could not decode JSON."); 
    exit; 
} 

if (isset($jsonArray['error'])) { 
    echo("An error has occurred: ". $jsonArray['error']); 
    exit; 
} 

if (!isset($jsonArray['access_token'])) { 
    echo("Access token not found."); 
    exit; 
} 

// Step 3: using access_token for youtube api call 
$message="Just Some Comment..."; 
$access_token= $jsonArray['access_token']; 
$video_id="I3LMKhu2-vo"; 
$message_xml='<?xml version="1.0" encoding="UTF-8"?> 
<entry xmlns="http://www.w3.org/2005/Atom"  xmlns:yt="http://gdata.youtube.com/schemas/2007"> 
<content>' . $message . '</content> 
</entry>'; 
$url = "https://gdata.youtube.com/feeds/api/videos/" . $video_id . "/comments"; 
$header = array('Content-Type: application/atom+xml', 'Content-Length: ' . strlen($message_xml), 'Authorization: Bearer "' . $access_token . '"', 'GData-Version: 2.1', 'X-GData-Key: key=' . $developer_key); 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $message_xml); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($ch); 
echo curl_error($ch); 
curl_close($ch); 

echo "DONE! Token:" . $access_token . "<br />\n"; 
var_dump($result); 
?> 

需要注意的是誰登錄到自己的谷歌賬戶,並代表對誰的評論將被張貼需要用戶有一個鏈接YouTube帳戶(僅限Google帳戶是不夠的)。此外,他還需要至少在YouTube上發佈至少一條評論。否則,他會看到類似「youtube_signup_required」或「NoLinkedYouTubeAccount」的錯誤。

我已經切換到API 2.1(GData版本),因爲它是更新的,並提供更好的功能和錯誤報告的情況下,谷歌帳戶未鏈接。

+0

嘿,非常感謝!我不得不編輯一行或兩行,因爲我得到一個關於「標題不能有多行」的錯誤,但這已經修復了! +50代表你的方式,並感謝您的幫助! – InfiniDaZa 2013-04-01 15:58:24

+0

不客氣!我很高興它解決了,謝謝你的聲音:) – Marcellus 2013-04-01 16:46:42