sferik / twitter-ruby

A Ruby interface to the Twitter API.
http://www.rubydoc.info/gems/twitter
MIT License
4.59k stars 1.31k forks source link

add media_category to make image upload return 'media_key' #1025

Closed rajyan closed 11 months ago

rajyan commented 1 year ago

Description

As explained in https://developer.twitter.com/en/docs/twitter-api/v1/media/upload-media/api-reference/post-media-upload

This endpoint returns a media_id by default and can optionally return a media_key when a media_category is specified. These values are used by Twitter endpoints that accept images.

We need to specify 'media_category' parameter to make the media/upload endpoint return 'media_key'. 'media_key' is required when using media in Twitter Ads API.

Testing

irb(main):049:0> Twitter::REST::Request.new(v1, :multipart_post, 'https://upload.twitter.com/1.1/media/upload.json', key: :media, file: tempfile, media_type: 'image/png', media_category: 'tweet_image').perform
=> {:media_id=>***, :media_id_string=>"***", :media_key=>"***", :size=>61702, :expires_after_secs=>86400, :image=>{:image_type=>"image/png", :w=>214, :h=>214}}
irb(main):050:0> Twitter::REST::Request.new(v1, :multipart_post, 'https://upload.twitter.com/1.1/media/upload.json', key: :media, file: tempfile, media_type: 'image/png').perform
=> {:media_id=>***, :media_id_string=>"***", :size=>61702, :expires_after_secs=>86400, :image=>{:image_type=>"image/png", :w=>214, :h=>214}}
rajyan commented 1 year ago

@sferik Hi, sorry for the ping. Could you please take a look? media/upload is still important after the Twitter's plan migration.