上传至 YouTube


YouTube 提供了一个浏览器上传 API,可以结合本插件一起使用:http://code.google.com/apis/youtube/1.0/developers_guide_python.html#BrowserUpload (Python) http://code.google.com/apis/youtube/1.0/developers_guide_php.html#BrowserUpload (PHP)

以下教程利用插件的 send API,先通过服务端程序提交文件元数据,然后将视频本身直接上传到 YouTube:

客户端


$('#fileupload').fileupload({
    forceIframeTransport: true,
    submit: function (e, data) {
        var that = $(this);
        // Post the current file meta data to your application,
        // to receive the YouTube upload url and token:
        // http://code.google.com/apis/youtube/1.0/developers_guide_python.html#BrowserUpload
        $.post('/your_application_url', data.files[0], function (result) {
            data.url = result.url;
            data.formData = {token: result.token};
            that.fileupload('send', data);
        });
        return false;
    }
});

由于 YouTube 只允许单个文件上传并且必须传一个文件名,因此页面代码相对应的文件 input 框也要改成下面这样:

<input type="file" name="file">

服务端


你需要拥有一个自己的 Youtube 账号频道,视频文件会上传到你的账号频道下(项目实际用户不需要拥有 Youtube 账号),换言之,你将不得不重拾已被弃用的 ClientLogin 身份校验方法。该方法允许你通过邮箱加密码来登录,然后你就能拿到访问令牌,并用它来调用 API 了。

PHP

下面是一段 PHP 的示例:

$EMAIL = YOUR_EMAIL;
$PASS = YOUR_PASS;
$ch = curl_init();
$header = array();
$header[] = "Content-type: application/x-www-form-urlencoded;";
curl_setopt_array($ch, array(
	CURLOPT_URL=>'https://www.google.com/accounts/ClientLogin',
	CURLOPT_HTTPHEADER=>$header,
	CURLOPT_RETURNTRANSFER=>true,
	CURLOPT_SSL_VERIFYPEER=>false,
	CURLOPT_SSL_VERIFYHOST=>false,
	CURLOPT_POST=>true,
	// source is needed for stats, so define it as you like
	CURLOPT_POSTFIELDS=>"Email=$EMAIL&Passwd=$PASS&service=youtube&source=SOURCE"
));

// the main token needed to make requests
// since it hasn't limit by time, you can store it somewhere and not log in each time
$authToken = trim(array_pop(explode('Auth=',curl_exec($ch))));
// developer key could be found in app console by creating any web app
// it can be called 'api key' or 'simple api key', any of them should work
$DEVELOPER_KEY = DEVELOPER_KEY;

curl_close($ch);

// we need to construct xml to interact
$post_body = '<?xml version="1.0"?>
	<entry xmlns="http://www.w3.org/2005/Atom"
	  xmlns:media="http://search.yahoo.com/mrss/"
	  xmlns:yt="http://gdata.youtube.com/schemas/2007">
	  <media:group>
		<media:title type="plain">TITLE_OF_VIDEO</media:title>
		<media:description type="plain">
		  Description
		</media:description>
		<media:category
		  scheme="http://gdata.youtube.com/schemas/2007/categories.cat">CATEGORY
		</media:category>
		<media:category
		  scheme="http://gdata.youtube.com/schemas/2007/categories.cat">SECOND_CATEGORY
		</media:category>
		<media:keywords>KEYWORDS_SEPARATED_BY_COMMA</media:keywords>
	  </media:group>
	</entry>';

$ch = curl_init();
$header = array();
$header[] = "X-GData-Key: key=$DEVELOPER_KEY";
$header[] = "Authorization: GoogleLogin auth=$authToken";
$header[] = "GData-Version: 2";
$header[] = "Content-length: ".strlen($post_body);
$header[] = "Content-type: application/atom+xml; charset=UTF-8";
curl_setopt_array($ch, array(
	CURLOPT_URL=>'gdata.youtube.com/action/GetUploadToken',
	CURLOPT_HTTPHEADER=>$header,
	CURLOPT_RETURNTRANSFER=>true,
	CURLOPT_POST=>true,
	CURLOPT_POSTFIELDS=>$post_body
));
$response = (array)simplexml_load_string(curl_exec($ch));
// we need to tune the url with necessary parameter 'nexturl'
// on which you'll get redirect with 'id' of video, 'status' (HTTP responce) and 'error' (if there is some) in GET
$result['url'] .= '?nexturl='.url_encode(YOUR_REDIRECT_PAGE);

// result will have 'token' and 'url'