我一直在用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;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我在帖子参数中用"url"代替"数据"时(按照他们的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',
]);
Run Code Online (Sandbox Code Playgroud)
根据这个答案/sf/answers/12450651/,“data[]”也可能在这种情况下工作。
如果 LWP::Useragent 尚未执行此操作,我还会考虑将“Content-type: application/x-www-form-urlencoded”添加到请求标头中。