4

我正在尝试在 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 上查看演示。所以我的问题是,为什么即使用户发送了授权令牌,视频上也没有出现评论?

4

1 回答 1

2

我认为您的代码中缺少的主要内容是您必须使用授权代码通过调用令牌服务来获取真正的 access_token(请参阅我的代码中的步骤 2)。这意味着您总共将有两个curl 请求。有关详细信息,请查看文档:https ://developers.google.com/accounts/docs/OAuth2WebServer?hl=de#handlingtheresponse

此外,您需要在https://code.google.com/apis/console/上创建一个项目(除非已经完成),以便为授权 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&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);
?>

请注意,登录其 google 帐户并代表将发布评论的用户需要有一个链接的 YouTube 帐户(仅 google 帐户是不够的)。此外,他需要在 YouTube 上成功发布至少一条评论。否则,他会看到类似“youtube_signup_required”或“NoLinkedYouTubeAccount”的错误。

而且我已经切换到 API 2.1 (GData-Version),因为它是更新的,并且在未链接的谷歌帐户的情况下提供了更好的功能和错误报告。

于 2013-04-01T13:49:29.080 回答