sferik / x-ruby

A Ruby interface to the X API.
MIT License
68 stars 13 forks source link

Media type unrecognized when uploading a short video #14

Closed reboot-mk closed 6 months ago

reboot-mk commented 6 months ago

Hello, here's my code:

require "x"
require "x/media_uploader"

credentials = {
    api_key:                "xxx",
    api_key_secret:         "xxx",
    access_token:           "xxx",
    access_token_secret:    "xxx",
}
client = X::Client.new(**credentials)

media = X::MediaUploader.upload(client: client, file_path: "test.mp4", media_category: "tweet_video")
tweet_body = {text: post_message, media: {media_ids: [media["media_id_string"]]}}
client.post("tweets", tweet_body.to_json)

Which throws:

<projet_path>vendor/bundle/ruby/3.0.0/gems/x-0.12.0/lib/x/response_parser.rb:48:in `parse': media type unrecognized. (X::BadRequest)
    from <projet_path>vendor/bundle/ruby/3.0.0/gems/x-0.12.0/lib/x/client.rb:120:in `execute_request'
    from <projet_path>vendor/bundle/ruby/3.0.0/gems/x-0.12.0/lib/x/client.rb:55:in `post'
    from <projet_path>vendor/bundle/ruby/3.0.0/gems/x-0.12.0/lib/x/media_uploader.rb:24:in `upload'
    from <projet_path>test.rb:12:in `<main>'

Here's test.mp4

I'm not sure what I'm doing wrong... This video file used to work with the previous Twitter gem, so I don't think it's a file format issue. I'm able to upload pictures, so I don't think it's an API access issue either.

I'm encountering a similar issue with gif files when uploading a gif and setting the type to tweet_gif. Although I managed to upload this gif file, but I couldn't play it with my browser or even the Android client.

GCorbel commented 6 months ago

Did you try with :

media = X::MediaUploader.chunked_upload(client: client, file_path: "test.mp4", media_category: "tweet_video")
X::MediaUploader.await_processing(client: client, media: media)
tweet_body = {text: post_message, media: {media_ids: [media["media_id_string"]]}}
client.post("tweets", tweet_body.to_json)

There is an example here : https://github.com/sferik/x-ruby/blob/main/examples/chunked_media_upload.rb

reboot-mk commented 6 months ago

It worked, thank you so much! My apologies for not trying this out sooner, my files were light so I didn't think I'd need to use chunked uploading. I did have to require tmpdir at the beginning of my file, not sure if that's intentional.