草庐IT

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

coder 2024-04-19 原文

当我尝试使用 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/

有关php - Youtube API 有什么问题(无效请求)?的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Rails - 子类化模型的设计模式是什么? - 2

    我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co

  3. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  4. ruby - 什么是填充的 Base64 编码字符串以及如何在 ruby​​ 中生成它们? - 2

    我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%

  5. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  6. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  7. ruby - 为什么 4.1%2 使用 Ruby 返回 0.0999999999999996?但是 4.2%2==0.2 - 2

    为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返

  8. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  9. ruby - ruby 中的 TOPLEVEL_BINDING 是什么? - 2

    它不等于主线程的binding,这个toplevel作用域是什么?此作用域与主线程中的binding有何不同?>ruby-e'putsTOPLEVEL_BINDING===binding'false 最佳答案 事实是,TOPLEVEL_BINDING始终引用Binding的预定义全局实例,而Kernel#binding创建的新实例>Binding每次封装当前执行上下文。在顶层,它们都包含相同的绑定(bind),但它们不是同一个对象,您无法使用==或===测试它们的绑定(bind)相等性。putsTOPLEVEL_BINDINGput

  10. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

随机推荐