为什么这个CURL请求不能工作?


Why won't this CURL request work?

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

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

此外,您需要在https://code.google.com/apis/console/上创建(除非已经完成)一个项目来创建Client IDClient secret用于授权API访问。除了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帐户(单独的谷歌帐户是不够的)。此外,他还需要在YouTube上成功发表至少一条评论。否则,他会看到一个错误,如"youtube_signup_required"或"NoLinkedYouTubeAccount"。

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