我正在基于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 = "[email protected]",
          $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] =>
)

最佳答案

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

在此处查找完整的类别列表

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

这是直接下载到.cat文件

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

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



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

好的,我建立了一个zend框架,这个代码对我有用

*此代码仅是解释性目的-不安全也不理想*

用户想要通过一个步骤表单上传视频,这是解决方案

    <?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;
    }

关于php - 基于YouTube api v3浏览器的上传引发400错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20839481/

10-12 04:34