skip to Main Content

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


  1. Chosen as BEST ANSWER

    After inspiring from this answer here.

    I had done the same in perl something like:

    my @filename = ('path/to/image.jepg');
    my $result = $nt->update_with_media({status=>'hI',media=>@filename});
    

    This worked perfectly. Although only array reference works here.

    Note: According to documentation and from @simbabque answer link

    upload upload(media)

    Parameters: media
    Required: media
    Upload images to twitter without posting them on the timeline

    Returns: Image

    Which I guess will only upload but doesnot show on timeline. So I used the update_with_media method to post images.


  2. The documentation is a bit sparse on upload, but it does say it needs something called media.

    upload

    upload(media)

    Parameters: media
    Required: media
    Upload images to twitter without posting them on the timeline

    Returns: Image

    However, directly above at update_with_media it describes an entity media.

    The media[] parameter is an arrayref with the following
    interpretation:

    [ $file ]
    [ $file, $filename ]
    [ $file, $filename, Content_Type => $mime_type ]
    [ undef, $filename, Content_Type => $mime_type, Content => $raw_image_data ]
    

    So based on that I would expect that your upload has to look something like this:

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

    Note that I have not tested this.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search