我正在使用Net::Twitter
(cpan link)提供的Twitter API。我可以发布状态,但无法上传图片。
Perl代码:
use strict;
use warnings;
use MIME::Base64;
use Net::Twitter;
use Scalar::Util 'blessed';
# When no authentication is required:
#my $nt = Net::Twitter->new(legacy => 0);
my $consumer_key = "consumerkey";
my $consumer_secret = "consumersecret";
my $token="token";
my $token_secret="tokensecret";
# As of 13-Aug-2010, Twitter requires OAuth for authenticated requests
my $nt = Net::Twitter->new(
traits => [qw/API::RESTv1_1/],
consumer_key => $consumer_key,
consumer_secret => $consumer_secret,
access_token => $token,
access_token_secret => $token_secret,
);
eval {
#my $result = $nt->update({status=>'some message'}); ##this works fine
my $result = $nt->upload({media=>'/some/path/toimage.jpeg' ,media_data => encode_base64('/some/path/toimage.jpeg')}); #this line produces error
};
if ( my $err = $@ ) {
die $@ unless blessed $err && $err->isa('Net::Twitter::Error');
warn "HTTP Response Code: ", $err->code, "\n",
"HTTP Message......: ", $err->message, "\n",
"Twitter error.....: ", $err->error, "\n";
}
错误:
HTTP Response Code: 400
HTTP Message......: Bad Request
Twitter error.....: media type unrecognized.
答案 0 :(得分:1)
从这个答案中鼓舞人心here。
我在Perl中做过同样的事情:
my @filename = ('path/to/image.jepg');
my $result = $nt->update_with_media({status=>'hI',media=>\@filename});
这完美无缺。虽然这里只有数组引用。
注意:根据文件和@simbabque回答link
上传上传(媒体)
参数:媒体
要求:媒体
将图片上传到Twitter而不在时间轴上发布返回:图片
我猜这只会上传,但不会在时间轴上显示。所以我使用update_with_media
方法发布图片。
答案 1 :(得分:0)
The documentation在media[]
上有点稀疏,但确实说它需要一些名为 media 的东西。
上传
上传(媒体)
参数:媒体
要求:媒体
将图像上传到Twitter而不在时间线上发布返回:图片
但是,在at update_with_media
正上方,它描述了一个实体 media 。
[ $file ] [ $file, $filename ] [ $file, $filename, Content_Type => $mime_type ] [ undef, $filename, Content_Type => $mime_type, Content => $raw_image_data ]
参数是一个带有以下内容的arrayref 解释:my content_of_image_file; # open the file and read its content in binary mode my $result = $nt->upload([ undef, 'filename_as_it_should_appear_on_twitter.jpeg', # this is a guess Content_Type => 'image/jpeg', Content => encode_base64($content_of_image_file), ]);
基于此,我希望您的上传内容必须如下所示:
private void removeTabWithTitle(String tabTitleToRemove) {
for (int i = 0; i < tabbedPane.getTabCount(); i++) {
String tabTitle = tabbedPane.getTitleAt(i);
if (tabTitle.equals(tabTitleToRemove)) {
tabbedPane.remove(i);
break;
}
}
}
请注意,我尚未对此进行测试。