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

前端之家收集整理的这篇文章主要介绍了使用perl通过API在tumblr上发布本地映像文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在用perl搞乱tumblr API,并且已经有了几个功能.

但是,我无法通过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;
   }

但是,当我将“url”替换为post参数中的“data”时(按照其API描述中的指示 – http://www.tumblr.com/docs/en/api/v2#posting),我不断收到tumblr的错误响应.我已经尝试了几种输入“数据”参数的方法 – 作为文件的路径,作为二进制表示,作为URL编码的二进制表示,作为url编码的base64二进制表示,将这些值中的一个作为唯一元素卡住在数组中 – 我已经尝试了所有,并且每一个我从tumblr收到一条错误消息.

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

解决方法

我并不完全熟悉tumblr API,但是谷歌快速搜索了这个例子: https://gist.github.com/derekg/1198576

我会尝试

$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',]);

根据这个答案https://stackoverflow.com/a/177866/810448,“data []”也可能在这种情况下起作用.

如果LWP :: Useragent尚未执行此操作,我还会考虑将“Content-type:application / x-www-form-urlencoded”添加到请求标头中.

猜你在找的Perl相关文章