Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The error occurs when twitter: image and twitter: text shared to twitter OAuth api #907

Closed
Rybots opened this issue Mar 9, 2018 · 2 comments

Comments

@Rybots
Copy link

Rybots commented Mar 9, 2018

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

@vladfreel
Copy link

vladfreel commented Nov 1, 2018

update_with_media deprecated.
My code:

> def send_to_twitter social_pictures, content
>     pictures = []
>     social_pictures.each do |picture|
>       pictures << picture.image.file.path
>     end
>     media_ids = pictures.map do |filename|
>       twitter_client.upload(File.new(filename))
>     end.map(&:value)
>     twitter_client.update(content, :media_ids => media_ids)
> end

But I have same error

@PashaLVWD
Copy link

@Rybots I think you need to copy metadata from image_file (StringIO) to your temp_file (Tempfile) as suggested here: https://gist.github.com/janko-m/7cd94b8b4dd113c2c193#file-02-safe-download-commented-rb-L58

@sferik sferik closed this as completed Sep 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants