I am using twitter API provided by Net::Twitter
(cpan link). I am able to post status but unable to upload image.
Perl code:
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";
}
Error:
HTTP Response Code: 400
HTTP Message......: Bad Request
Twitter error.....: media type unrecognized.
2
Answers
After inspiring from this answer here.
I had done the same in perl something like:
This worked perfectly. Although only array reference works here.
Note: According to documentation and from @simbabque answer link
Which I guess will only upload but doesnot show on timeline. So I used the
update_with_media
method to post images.The documentation is a bit sparse on
upload
, but it does say it needs something called media.However, directly above at
update_with_media
it describes an entity media.So based on that I would expect that your upload has to look something like this:
Note that I have not tested this.