php - Youtube API 有什么问题(无效请求)?

标签 php youtube-api

当我尝试使用 youtube API 上传视频时,出现“无效请求”错误。

但是!

当我上传任何“图片”而不是“视频”时,它会返回状态“200”和视频 ID。

为什么我无法上传视频?

$eq = 'accountType=HOSTED_OR_GOOGLE&Email='.$YOUTUBE_EMAIL.'&Passwd='.$YOUTUBE_PASS.'&service=youtube&source='.$API_NAME;
if ($fp = fsockopen ("ssl://www.google.com", 443, $errno, $errstr, 20))
{
    $request ="POST /youtube/accounts/ClientLogin HTTP/1.0\r\n";
    $request.="Host: www.google.com\r\n";
    $request.="Content-Type:application/x-www-form-urlencoded\r\n";
    $request.="Content-Length: ".strlen($eq)."\r\n";
    $request.="\r\n\r\n";
    $request.=$eq;
    fwrite($fp,$request,strlen($request));

    while (!feof($fp))
    $response.=fread($fp,8192);
    //fclose($fp);
}

preg_match("!(.*?)Auth=(.*?)\n!si",$response,$ok);
$AUTH_TOKEN = $ok[2];

$data = "<?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'>test</media:title>
     <media:description type='plain'>test</media:description>
     <media:category scheme='http://gdata.youtube.com/schema /2007/categories.cat'>People</media:category>
     <media:keywords>toast, wedding</media:keywords>
    </media:group>
</entry> ";

if ($fp = fsockopen ("gdata.youtube.com", 80, $errno, $errstr, 20))
{
    $request ="POST /action/GetUploadToken HTTP/1.1\r\n";
    $request.="Host: gdata.youtube.com\r\n";
    $request.="Content-Type: application/atom+xml; charset=UTF-8\r\n";
    $request.="Content-Length: ".strlen($data)."\r\n";
    $request .="Authorization: GoogleLogin auth=".$AUTH_TOKEN."\r\n";
    $request.="X-GData-Client: ".$API_NAME." \r\n";
    $request.="X-GData-Key: key=".$API_KEY." \r\n";
    $request.="\r\n";
    $request.=$data."\r\n";

    socket_set_timeout($fp, 10);
    fputs($fp,$request,strlen($request));
    $response = fread($fp,3280);
    fclose($fp);
}


preg_match('|<url>(.*)</url>|Uis', $response, $url);
preg_match('|<token>(.*)</token>|Uis', $response, $token);

print "
    <form action='".$url[1]."?nexturl=http%3A%2F%2Fwww.google.com' method='post' enctype='multipart/form-data'>
      <input type='file' name='file'>
      <input type='hidden' name='token' value='".$token[1]."'>
      <input type='submit' value='go'>
    </form> ";

最佳答案

哈哈哈!!将近一年后,我想我找到了解决办法。

你的行在“/schema”之后和“/2007”之前有一个空格

<media:category scheme='http://gdata.youtube.com/schema /2007/categories.cat'>People</media:category>

应该是

<media:category scheme='http://gdata.youtube.com/schema/2007/categories.cat'>People</media:category>

查看下面的第二个代码块:https://developers.google.com/youtube/2.0/developers_guide_protocol_direct_uploading#Sending_a_Direct_Upload_API_Request

关于php - Youtube API 有什么问题(无效请求)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6450950/

相关文章:

javascript - 从 php 重定向后无法运行 <Script>

php - 使用数组从多个 mysql 表中删除行

android - YouTubePlayerView 滚动时出现黑色轮廓

php - 使用 PHP 在 SQlite3 中保存十进制值

javascript - 使用 php 在 AJAX 中进行长轮询

php - 限制用户角色仅更改 Woocommerce 中的某些订单状态

javascript - YouTube API 更改事件未触发

php - YouTube API v3 未检索 channel 的视频

firefox - YouTube iFrame API 'onStateChange' 未在 Firefox 中触发

javascript - 排队 YouTube 视频