2013-03-27 100 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