Unable to upload any images?

media
media-upload
api

#1

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!!


#2

It looks like the method should be update_with_media (not upload). Note that this API is deprecated and has been replaced with a newer media upload endpoint on the Twitter side, but I don’t see any evidence that the Perl module has got any support for that yet.


#3

Hi Andy!

Thank you for responding!
Ah, I didn’t notice that typo…
I just corrected it and ran via CMD line, but got the 403 Forbidden error.

I understand it’s deprecated but from what I’ve read over the past 2 days, it’s still OK to use if you only want to upload a single image (which we do).

Any other ideas?


#4

Do you get an error message back from the API call? HTTP 403 can refer to several of the error codes.


#5

Hi Andy,

No, I get ‘Error tweeting the text of my tweet + image file path/image.jpg 403 Forbidden 403: Forbidden at posttotwitter.pl

So 403 is all I have to go on!


#6

Take a look at the error handling section in the Perl module docs, you should be able to extract the API error message too. I’m not very familiar with this code, unfortunately, but @semifor (the author) is usually a very helpful person :slight_smile: