Youtube api v3浏览器基于上传抛出400错误

时间:2013-12-30 12:22:36

标签: php youtube youtube-api

我正在开发基于youtube浏览器的视频上传。但它抛出了一个错误。

Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 400 <?xml version='1.0' encoding='UTF-8'?><errors><error><domain>yt:validation</domain><code>invalid_value</code><location type='xpath'>media:group/media:category[@scheme='http://gdata.youtube.com/schemas/2007/categories.cat']/text()</location></error></errors>' in /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php:714 Stack trace: #0 /home/xxx/public_html/yyy.com/up/Zend/Gdata.php(219): Zend_Gdata_App->performHttpRequest('POST', 'http://gdata.yo...', Array, '<atom:entry xml...', 'application/ato...', NULL) #1 /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php(905): Zend_Gdata->performHttpRequest('POST', 'http://gdata.yo...', Array, '<atom:entry xml...', 'application/ato...') #2 /home/xxx/public_html/yyy.com/up/Zend/Gdata/YouTube.php(704): Zend_Gdata_App->post(Object(Zend_Gdata_YouTube_VideoEntry), 'http://gdata.yo...') #3 /home/xxx/public_html/yyy.com/up/video_upload. in /home/xxx/public_html/yyy.com/up/Zend/Gdata/App.php on line 714

代码:

<?php
// video_upload.php


require_once 'Zend/Loader.php'; // the Zend dir must be in your include_path
Zend_Loader::loadClass('Zend_Gdata_YouTube');
$yt = new Zend_Gdata_YouTube();

// Define the authentication that will be used
Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); 

// Authenticate
$authenticationURL= 'https://www.google.com/accounts/ClientLogin';
$httpClient = 
Zend_Gdata_ClientLogin::getHttpClient(
          $username = "example@gmail.com",
          $password = "example",
          $service = 'youtube',
          $client = null,
          $source = 'HTML SOURCE CODE SNIPPET',
          $loginToken = null,
          $loginCaptcha = null,
          $authenticationURL);

$applicationId = 'YOUR APPLICATION ID';
$clientId = 'Upload videos to youtube using the youtube API';
$developerKey = 'A...........................0';
$yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);



// create a new VideoEntry object
$myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

$myVideoEntry->setVideoTitle('My');
$myVideoEntry->setVideoDescription('video');
// The category must be a valid YouTube category!
$myVideoEntry->setVideoCategory('news');

// Set keywords. Please note that this must be a comma-separated string
// and that individual keywords cannot contain whitespace
$myVideoEntry->SetVideoTags('news');

$tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
$tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
$tokenValue = $tokenArray['token'];
$postUrl = $tokenArray['url'];


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl."?nexturl=//example.com/up");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file">
$post = array("file"=>"@".$VideoFile['tmp_name'], "token"=>$tokenValue);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
$response = curl_exec($ch);
$info = curl_getinfo($ch);

echo $info;
?>

形式:

    <form action="video_upload.php" method="post" enctype="multipart/form-data">
    <input type="file" name="uploadedfile"><br>
    <input type="submit" value="Upload File to Server">
    </form>

我想直接将视频上传到我的YouTube帐户。用户可以将视频上传到我的YouTube帐户..

所以我没有向$applicationId$clientId添加任何内容。

我认为需要使用$applicationId$clientId完成某些事情。

由于

更新

正如 Wanny Miarelli 建议的那样 我将news更改为News,现在没有显示错误,但视频也未上传。

尝试print_r($info)并获得此

Array
(
   [url] => http://uploads.gdata.youtube.com/action/FormDataUpload/AIwbFAS1SkX04C09Re5sWeYXb562vc7W5KHcRu0uZcJ7v-A4nX_21zVwIOxdeBtp_s4tj-UcnZxJaN-Zd7nKVUfWwtWxW2YEPg?nexturl=//example.com/up
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.024856
    [namelookup_time] => 0.018251
    [connect_time] => 0.024751
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 
)

1 个答案:

答案 0 :(得分:1)

是的,似乎是一个类别问题。尝试将“新闻”更改为“新闻”

查看完整的类别列表

https://developers.google.com/youtube/2.0/reference#Region_specific_feeds

这是直接下载到.cat文件

http://gdata.youtube.com/schemas/2007/categories.cat

这是Goolge在视频上传(成功或失败)后应该发送给您的

  
    

上传视频文件后,用户将被重定向到表单中指定的nexturl。如果上传成功,则为YouTube     将id和status参数附加到URL,如下所示     例如:

  
     

http://www.example.com/youtube_uploads?status=200&id=JPF-DXF7hzc如果   上传不成功,YouTube会附加状态和代码   URL的参数可以帮助您诊断失败的原因。   参考指南提供了有关这些参数的更多信息。

好的,我设置了一个zend框架,这段代码适用于我

* 此代码仅供参考 - 不安全且不优惠*

用户想要使用One Step Form上传视频,这是解决方案

    <?php

            //single file script, need to know if we are in a POST or GET request, this needs to be changed, dividing the script into a separate file 

            if(isset($_FILES['file']['name'])){


              Zend_Loader::loadClass('Zend_Gdata_YouTube');
              Zend_Loader::loadClass('Zend_Gdata_ClientLogin');

              $authenticationURL= 'https://www.google.com/accounts/ClientLogin';

              $httpClient = Zend_Gdata_ClientLogin::getHttpClient(
                            $username = '',
                            $password = '',
                            $service = 'youtube',
                            $client = null,
                            $source = 'TestApp', // a short string identifying your application
                            $loginToken = null,
                            $loginCaptcha = null,
                            $authenticationURL);


              $applicationId = 'TestAPP';
              $developerKey = "---";
              $clientId = "";

              // Note that this example creates an unversioned service object.
              // You do not need to specify a version number to upload content
              // since the upload behavior is the same for all API versions.
              $yt = new Zend_Gdata_YouTube($httpClient, $applicationId, $clientId, $developerKey);

              // create a new VideoEntry object
              $myVideoEntry = new Zend_Gdata_YouTube_VideoEntry();

              $myVideoEntry->setVideoTitle($_POST['Title']);
              $myVideoEntry->setVideoDescription($_POST['Description']);
              // The category must be a valid YouTube category!
              $myVideoEntry->setVideoCategory('News');

              // Set keywords. Please note that this must be a comma-separated string
              // and that individual keywords cannot contain whitespace
              $myVideoEntry->SetVideoTags('News');

              $tokenHandlerUrl = 'http://gdata.youtube.com/action/GetUploadToken';
              $tokenArray = $yt->getFormUploadToken($myVideoEntry, $tokenHandlerUrl);
              $tokenValue = $tokenArray['token'];
              $postUrl = $tokenArray['url'];

              // place to redirect user after upload
              $nextUrl = 'http://localweb/';

              $target_url = 'http://127.0.0.1/accept.php';
            //This needs to be the full path to the file you want to send.
                $file_name_with_full_path = realpath('C:\video.flv');
                      /* curl will accept an array here too.
                       * Many examples I found showed a url-encoded string instead.
                       * Take note that the 'key' in the array will be the key that                    shows up in the
                       * $_FILES array of the accept script. and the at sign '@' is required before the
                       * file name.
                       */

                 // Post the video to Youtube Server
                $post = array('token' => $tokenValue,'file_contents'=>'@'. $_FILES["file"]["tmp_name"]);

                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $postUrl . '?nexturl=' . $nextUrl);
                curl_setopt($ch, CURLOPT_POST,1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                $result=curl_exec ($ch);
                curl_close ($ch);
                echo $result;


    }else{

      $self = $_SERVER['PHP_SELF'];

      $form = '<form action="' . $self  . '"'.
            ' method="post" enctype="multipart/form-data">'. 
            '<input name="file" id="file" type="file"/>'. 
            '<input name="token" id="token" type="hidden" value=""/>'.
            '<input name="Description" id="Description" />'.
            '<input name="Title" id="Title" />'.
            '<input value="Upload Video File" type="submit" />'. 
            '</form>';

            echo $form;
    }