The error occurs when twitter: image and twitter: text shared to twitter

api

#1

Dear,

I have the website which user can share some own image with text to Twitter by using Twitter OAuth API.
(Ruby:2.3.6,Rails: 5.1.4,‘omniauth-twitter’ gem,Amazon Linux AMI)

and, when User is trying to tweet with an image,
(using this)

client.update_with_media(
  comment,
  image_path
 )

sometimes the error occurs with following messages

[DEPRECATED] :mime_type option deprecated, use :content_type
Twitter::Error::Unauthorized (Invalid or expired token.):
Completed 500 Internal Server Error

Then, I searched and I found out that If the image is smaller than 10 kb, image path is recognized as Twitter as a character string.
So I wrote monkey patch like this.

 module Twitter::Image
    def self.open_from_url(image_path)                                                                                          
         image_file = open(image_path)
         return image_file unless image_file.is_a?(StringIO)
         file_name = File.basename(image_path)
         temp_file = Tempfile.new(file_name)
         temp_file.binmode
         temp_file.write(image_file.read)
         temp_file.close
         open(temp_file.path)
    end       
  end  

 image_path = Twitter::Image.open_from_url(image_path)
 client.update_with_media(
     comment + "\nhhbox.net/#{user.screen_name}",
     image_path
 )

However, It doesn’t work well.Sometimes errors occur yet.
Could you please help??

Thanks