我正在尝试在YouTube视频上发表评论...为此,我使用了YouTube API。这是代码:
<?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;
?>
我在哪里引用<!---visit demo for actual code---!>
隐藏个人资料,但您可以在votm.net78.net上看到演示。所以我的问题是,即使用户发送了授权令牌,为什么评论不会出现在视频中?
答案 0 :(得分:2)
我认为缺少的主要是你的代码是你必须通过调用令牌服务来使用授权代码来获取真正的access_token(参见我的代码中的第2步)。这意味着您总共会有两个 curl请求。有关详细信息,请查看文档:{{3}}
此外,您需要在https://developers.google.com/accounts/docs/OAuth2WebServer?hl=de#handlingtheresponse上创建(除非已经完成)项目,以便为授权API访问创建Client ID
和Client 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…');
} 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);
?>
请注意,登录其Google帐户并代表评论将被发布的用户需要拥有关联的YouTube帐户(仅谷歌帐户是不够的)。此外,他需要在YouTube上成功发布至少一条评论。否则,他会看到“youtube_signup_required”或“NoLinkedYouTubeAccount”之类的错误。
我已经切换到API 2.1(GData-Version),因为它更新,并且在未链接的Google帐户的情况下提供更好的功能和错误报告。