php代码评论youtube视频

时间:2013-12-24 11:52:10

标签: php youtube

我正在使用以下代码在youtube视频上添加评论。 请有人能告诉我这是什么问题。每次出现“无效令牌401”错误。 我已经检查了访问令牌是否正常,还有其他东西我已经签出。

和数组值为:

Array
(
    [0] => Content-Type: application/atom+xml
    [1] => Host: gdata.youtube.com
    [2] => Authorization: Bearer {"access_token":"ya29.1.AADtN_XE6XaTVkDVdxQftAQHexp-svuVuo3QxJm_NHTKxqs8hLrg6-CDwODwv1xhsgNocDNB","token_type":"Bearer","expires_in":3600,"id_token":"eyJhbGciOiJSUzI1NiIsImtpZCI6IjQ2YmFlYjMyM2M3NTk0MmE1MGNlMTlkY2VjNWJkYmEzYjQ5N2JjN2QifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwiYXVkIjoiMjc0MjM1OTYzMjkwLmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwic3ViIjoiMTA2MzI3ODAyMDQxODE3Mjc0ODk3IiwiYXRfaGFzaCI6InlQa05uSjVlM3N1S1FZZ0dnTzVBd1EiLCJhenAiOiIyNzQyMzU5NjMyOTAuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJpYXQiOjEzODczNTM4MzgsImV4cCI6MTM4NzM1NzczOH0.iIf3PjoN-AtE3kDXc60baEn0B0bH92JYKbB6H9JboNIOqp7oqQ0OVnFYs00e8RICDI2w7mFXzNOGNB6xvHGtpzyDSHbN4fa2phTCe5T7EVqK-AiuAQEeAPdIItgxGOZOyWvNDyIJ0J3vYGuovvoLTtDj9yEEKbYpBNxgGl8g-gA","refresh_token":"1\/7ZeGbxeUWJ7I7Rhy-ArVl9yzLYR7fgCtI3py6GU9BA8","created":"1387354138"}
    [3] => GData-Version: 2
    [4] => Content-Length: 22
    [5] => X-GData-Key: key=AIzaSyBeh0Aevex7q3iRIY5bV3N9gx0WAkNBMi4
)

我的代码从这里开始:

$g_access_tokens = array();
$g_access_tokens = array();
$g_access_tokens[0] = array("access_token" => $sKey3, "token_type" => "Bearer", "expires_in" => 3600, "id_token" =>

    $sKey2, "refresh_token" => $sKey4, "created" => $sKey1);


$_SESSION['access_token'] = $g_access_tokens;
$g_access_tokens = json_encode($_SESSION['access_token'][0]);
$client->setAccessToken($g_access_tokens);
$data = '<?xml version="1.0" encoding="UTF-8"?>
                <entry xmlns="http://www.w3.org/2005/Atom"
                    xmlns:yt="http://gdata.youtube.com/schemas/2007">
                  <content>This is a crazy video.</content>
                </entry>
                ';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/feeds/api/videos/' . $postid . '/comments');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
echo 'http://gdata.youtube.com/feeds/api/videos/' . $postid . '/comments';
echo "<pre>";
$content = array('Content-Type: application/atom+xml',
    'Host: gdata.youtube.com',
    'Authorization: Bearer ' . $g_access_tokens,
    'GData-Version: 2',
    'Content-Length: 22',
    'X-GData-Key: key=' . $keys["AppKey"]);
print_r($content);
curl_setopt($ch, CURLOPT_HTTPHEADER, $content);
$re = curl_exec($ch);
curl_close($ch);

1 个答案:

答案 0 :(得分:0)

最近我也面临这个问题......

我在您的代码中发现了3个问题

  1. 令牌应使用access_token而不是json string
  2. 帖子需要使用https,一个安全的连接
  3. content-length应使用完整的xml长度
  4. 确保启用了youtube api并添加了youtube范围

            $videoId = 'FglqN1jd1tM';
            $g_access_tokens = '<GOOGLE OAUTH 2.0 TOKEN>';//in json format
            $data = '<?xml version="1.0" encoding="UTF-8"?>
                            <entry xmlns="http://www.w3.org/2005/Atom"
                                xmlns:yt="http://gdata.youtube.com/schemas/2007">
                              <content>This is a crazy video.</content>
                            </entry>
                            ';
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://gdata.youtube.com/feeds/api/videos/' . $videoId . '/comments');
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            echo 'https://gdata.youtube.com/feeds/api/videos/' . $videoId . '/comments';
            echo "<pre>";
            $content = array(
                'POST /feeds/api/videos/'.$videoId.'/comments HTTP/1.1',
                'Host: gdata.youtube.com',
                'Content-Type: application/atom+xml',
                'Authorization: Bearer ' . json_decode($g_access_tokens)->access_token,
                'GData-Version: 2',
                'Content-Length: '.mb_strlen($data),
                'X-GData-Key: key=' . '<GOOGLE API PUBLICK KEY>');
            print_r($content);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $content);
            $re = curl_exec($ch);
            curl_close($ch);
            var_dump($re);