Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用perl通过API在tumblr上发布本地图像文件_Perl_Api_Tumblr - Fatal编程技术网

使用perl通过API在tumblr上发布本地图像文件

使用perl通过API在tumblr上发布本地图像文件,perl,api,tumblr,Perl,Api,Tumblr,我一直在用perl处理TumblrAPI,并得到了几个函数 但是,我无法通过perl上传本地图像文件 这是我的URL代码 use LWP::Authen::OAuth; use JSON; use Data::Dumper; use strict; my $ua = LWP::Authen::OAuth->new( oauth_consumer_key => 'xxx', oauth

我一直在用perl处理TumblrAPI,并得到了几个函数

但是,我无法通过perl上传本地图像文件

这是我的URL代码

    use LWP::Authen::OAuth;
    use JSON;
    use Data::Dumper;
    use strict;
         my $ua = LWP::Authen::OAuth->new(
             oauth_consumer_key => 'xxx',
             oauth_consumer_secret => 'xxx',
             oauth_token => 'xxx',
             oauth_token_secret => 'xxx',
     );




    my $response;
    $response = $ua->post( 'http://api.tumblr.com/v2/blog/mytumblr.tumblr.com/post', [
         type    => 'photo',
         url    => 'http://www.example.com/mypic.jpg' ,
         caption => 'Test image 1',
     ]);


    if ($response->is_success) {
      print "it worked";
    }
    else {
     print "it did not work \n \n \n \n";
     print $response->as_string;
   }
然而,当我在post参数中用“url”替换“data”时(如此处API描述中所示),我不断从tumblr得到错误响应。我尝试了几种输入“data”参数的方法——作为文件的路径,作为二进制表示,作为URL编码的二进制表示,作为URL编码的base64二进制表示,将其中的一个值作为数组中的唯一元素——我尝试了所有方法,每种方法都从tumblr返回一条错误消息


那么,有人能告诉我如何将本地图像文件上传到tumblr吗?

我对tumblr API不太熟悉,但通过谷歌快速搜索发现了以下示例:

我会试试看

$response = $ua->post( 'http://api.tumblr.com/v2/blog/mytumblr.tumblr.com/post', [
     type    => 'photo',
     'data[0]'    => $file_contents , ## LWP::Useragent should automatically urlencode this
     caption => 'Test image 1',
 ]);
根据这个答案,“数据[]”也可能在这种情况下起作用


我也会考虑向请求报头添加“内容类型:应用程序/XWW-FALU-URLCONDEND”,如果LWP::USER代理已经不在了。

您得到了什么错误?<代码> HTTP / 1.1 400坏请求连接:关闭日期:星期三,2013年5月15日21:48:31 GMT服务器:NGNIX内容类型:应用/JSON;charset=utf-8客户端日期:2013年5月15日星期三21:48:31 GMT客户端对等方:66.6.40.14:80客户端响应编号:1客户端传输编码:分块P3P:CP=“所有ADM开发PSAi COM我们的OTRo STP IND ONL”Set Cookie:tmgioct=519402aea446110439257710;expires=2023年5月13日星期六格林威治标准时间21:48:30;路径=/;httponly{“meta”:{“status”:400,“msg”:“Bad Request”},“response”:{“errors”:[“Error upload photo.”]}尝试删除标题部分-是否有效?这似乎也不起作用-但是这次我似乎收到以下错误:HTTP/1.1 401未授权连接:关闭日期:Thu,2013年5月16日17:48:56 GMT服务器:Apache Variy:接受编码内容长度:60内容类型:应用程序/json客户端日期:2013年5月16日星期四17:48:59 GMT客户端对等方:66.6.40.14:80客户端响应编号:1客户端警告:缺少身份验证头P3P:CP=“所有ADM开发PSAi COM我们的OTRo STP IND ONL”设置Cookie:tmgioct=51951c0b8ea0630258123660;expires=太阳,2023年5月14日17:48:59 GMT;路径=/;httponly X-Tumblr-Usec:D=3649265{“meta”:{“status”:401,“msg”:“notauthorized”},“response”:[]}还有两个后续问题-您使用什么来获取文件内容?您如何在UA post中设置内容类型?我尝试了几种方法(File::Slurp,my$imagepath='C:\scripts\vega1.jpg';open(IMAGE,$imagepath)或die“$!”;my$raw_string=do{local$/=undef;;};)用于文件上传,使用$ua->default_头(“'Content-type'=>'application/x-www-form-urlencoded”);,或者将其指定为POST请求中的参数-所有结果都是空的。老实说,我有点困惑