Hello!
Am new to Twitter API development, and also to Perl.
I have created a script to upload a tweet from a user interface that also posts to our website, using the Net::Twitter::LiteWith1_1 module.
At the moment, the script works absolutely fine for uploading text-only tweets, but whenever I try to upload an image, I get errors:
- either 403 Forbidden, or
- Can’t locate object method “code” via package “Can’t locate object method “upload_with_media” via package “Net::Twitter::Lite::WithAPIv1_1” at posttotwitter2.pl
line 61.” (perhaps you forgot to load “Can’t locate object method “upload_with_media” via package “Net::Twitter::Lite::WithAPIv1_1” at posttotwitter2.pl line 61.”?) at posttotwitter2.pl line 65.
I’ve tried breaking the code down to do the 2 step upload process, but I think I’m getting it wrong… If ANYONE can advise me, that’d be awesome!!
My code is below (minus access tokens and file paths, etc), if anyone wants to try it:
$text = 'Test with image';
@image = ('74_header-background-14.jpg', 'C\:\\path\\to\\file\\here\\');
tweet($text, @image);
sub tweet {
my ($text, $image) = @_;
unless ($text) {
die 'tweet requires text arguments';
}
unless ($consumer_key && $consumer_secret && $access_key && $access_secret) {
die 'Required Twitter Env vars are not all defined';
}
# build tweet, max 140 chars
my $tweet;
if (length($text) <= 140) {
$tweet = "$text ";
}
else {
# shorten text to 140 char limit
$tweet = substr($text, 0, 140);
}
try {
my $twitter = Net::Twitter::Lite::WithAPIv1_1->new(
access_token_secret => $access_secret,
consumer_secret => $consumer_secret,
access_token => $access_key,
consumer_key => $consumer_key,
user_agent => 'OwensGroupTestBot',
ssl => 1,
);
if ($image eq "") {
$twitter->update($tweet);
}
else {
$twitter->update_with_media(@image);
}
}
catch {
die join(' ', "Error tweeting $text $image",
$_->code, $_->message, $_->error);
};
}
Thank you!!