1

我正在尝试使用 perl 在 Youtube 中自动上传视频,我一直在阅读: https ://developers.google.com/youtube/2.0/developers_guide_protocol_resumable_uploads

并发现可恢复上传更容易手动完成,因为我不使用任何库,我在 perl 上找不到让我使用直接上传的库..

无论如何我已经完成了身份验证,但我的主要问题是上传部分,

到目前为止,这就是我所拥有的:

#!/usr/bin/perl

use 5.010;
use strict;
use warnings;
use LWP::Simple;
use Data::Dumper;
use XML::XPath;
my $token;


my $post_body ;


sub getToken {
    my %parms = @_;
    my $response =
      LWP::UserAgent->new->post(
                          'https://www.google.com/youtube/accounts/ClientLogin',
                          [
                            Email   => $parms{'username'},
                            Passwd  => $parms{'password'},
                            service => "youtube",
                            source  => "<<Your Value Here>>",
                          ]
      );

    my $content = $response->content;
   # print $content;
    my ($auth) = $content =~ /^Auth=(.*)YouTubeUser(.*)$/msg
      or die "Unable to authenticate...\n";
    my ($user) = $content =~ /YouTubeUser=(.*)$/msg
      or die "Could not extract user name from response string. ";

    return ( $auth );
}

## Get $AuthToken
($token) =
  getToken(
            (
              username => 'xxxx',
              password => 'xxxx'
            )
  );


##upload part

#xml
my $xml = <<'XML';
<?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">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    </media:description>
    <media:category
      scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
    </media:category>
    <media:keywords>toast, wedding</media:keywords>
  </media:group>
</entry>
XML



my $r = LWP::UserAgent->new->post(
    'http://uploads.gdata.youtube.com/resumable/feeds/api/users/xxxx/uploads',
    [
        Host              => 'uploads.gdata.youtube.com',
        'Authorization'     => "Bearer $token",
        'GData-Version'     => '2',
        'X-GData-Key'       => 'key=xxxxxxxxxxxxxx',    
        'Content-Length'    => '0',
        'Slug'              => 'C:\x\x.wmv',
        'Content-Type'  => 'application/atom+xml; charset=UTF-8'
    ],

    $xml,

);


    print Dumper( \$r->content );

结果:

$VAR1 = \'Content-Type application/x-www-form-urlencoded is not a valid input type.';

和..

$VAR1 = \'<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-wi
dth">
  <title>Error 400 (Bad Request)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{backgrou
nd:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height
:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/error
s/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflo
w:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (m
ax-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0
}}
  </style>
  <a href=//www.google.com/><img src=//www.google.com/images/errors/logo_sm.gif
alt=Google></a>
  <p><b>400.</b> <ins>ThatΓÇÖs an error.</ins>
  <p>Your client has issued a malformed or illegal request.  <ins>ThatΓÇÖs all w
e know.</ins>
';
Press any key to continue . . .
4

1 回答 1

0

对该方法的第二次调用中的参数post似乎是错误的 ( my $r = ...)。请参阅LWP::UserAgent->post 的 API。对数组或散列的引用将用于表单数据,但您将其用于 HTTP 标头。尝试以下操作:

  • 删除方括号(以便将标头插入为数组,而不是对数组的引用);
  • 替换$xml'Content' => $xml. (实际上可能没有必要,但根据 API 的字面理解,它是必要的。)
于 2013-09-28T23:32:52.990 回答