2013-09-28 30 views
1

我試着用perl YouTube數據自動上傳視頻, 嗯,我一直在閱讀這樣的: https://developers.google.com/youtube/2.0/developers_guide_protocol_resumable_uploads的perl上傳到YouTube

,並發現斷點續傳更容易通過手, 做的手,我不使用任何圖書館,我無法找到一個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 . . . 
+1

您應該考慮使用[3.0版API](https://developers.google.com/youtube/v3/引導件/ using_resumable_upload_protocol)。 – nwellnhof

回答

0

在第二呼叫到post方法的參數似乎錯誤(my $r = ...)。請參閱API for LWP::UserAgent->post。對數組或哈希的引用將用於表單數據,但是您將其用於HTTP標頭。請嘗試以下操作:

  • 刪除方括號(以便將標頭插入爲數組,而不是對數組的引用);
  • 替換$xml'Content' => $xml。 (實際上可能並不是必須的,但在API的文字閱讀下是必要的。)