Swifter icon indicating copy to clipboard operation
Swifter copied to clipboard

Post Tweet with Media

Open ramikalai opened this issue 4 years ago • 11 comments

I want to post a tweet that only contains an mp4 video.

Here's what I'm doing:

self.swifter!.postMultipartMedia(data!, type: .mp4, category: .video, success: {json in
    print("success: ", json)
    self.swifter!.postTweet(status: "", mediaIDs: [String(json["media_id_string"].string!)], success: { json in
        print("success 2: ", json)
    }, failure: { error in
        print("failure 2: ", error)
    })
}, failure: {error in
    print("failure: ", error)
 })

swifter!.postMultipartMedia works fine and returns:

{
  "processing_info" : {
    "state" : "succeeded"
  },
  "size" : 64845.0,
  "media_id" : 1.2888828910201733e+18,
  "media_key" : "7_1288882891020173313",
  "expires_after_secs" : 86400.0,
  "media_id_string" : "1288882891020173313"
}

The call to swifter!.postTweet fails though with this error:

message: "HTTP Status 400: Bad Request, Response: {\"errors\":[{\"code\":324,\"message\":\"Duration too short, minimum:500, actual:67

I'm familiar with the Twitter API generally, however I haven't encountered an error like this before... Was hoping to get some info on this

ramikalai avatar Jul 30 '20 17:07 ramikalai

@ramikalai
Sorry, I forgot to call appendUpload() method recursively. I think the video file is broken because the entire chunk hasn't posted. I will fix it and pull a request.

Kyome22 avatar Jul 31 '20 00:07 Kyome22

@ramikalai I have already fixed it and Requested pull my commit.

@mattdonnelly I would appreciate it if you check and merge my request.

Kyome22 avatar Jul 31 '20 02:07 Kyome22

I just checked out and merged the PR. Hopefully the issue is addressed.

meteochu avatar Jul 31 '20 04:07 meteochu

@Kyome22 Thank you for pushing a patch so quickly! I have just tried it out and unfortunately, I still get the same error... It's really strange as I expected your patch to fix the issue.

Have you managed to postTweet media uploaded through the postMultipartMedia endpoint?

Any thoughts on what else it could be?

ramikalai avatar Jul 31 '20 09:07 ramikalai

I figured out the issue - sending media as a tweet doesn't work well with the media_category optional...

I unset it, and everything worked! I've opened a PR #316 - would appreciate if you could merge this @meteochu

Thanks for the help @Kyome22 !

ramikalai avatar Jul 31 '20 23:07 ramikalai

@ramikalai Does the video file you use for testing comply with the "Media best practices"? https://developer.twitter.com/en/docs/media/upload-media/uploading-media/media-best-practices

I think that you should give MediaCategory as much as possible.

Kyome22 avatar Aug 01 '20 02:08 Kyome22

And I think attachments should be inspected before posting in the first place and users should be warned if they don't comply with media-best-practices.

Kyome22 avatar Aug 01 '20 02:08 Kyome22

@ramikalai Did you get the sync response instead of async when calling FINALIZE? My code certainly seems to be lacking the process for the case of the sync operation.

https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-finalize https://developer.twitter.com/en/docs/media/upload-media/api-reference/get-media-upload-status

Kyome22 avatar Aug 01 '20 03:08 Kyome22

@ramikalai

message: "HTTP Status 400: Bad Request, Response: {\"errors\":[{\"code\":324,\"message\":\"Duration too short, minimum:500, actual:67

Isn't the problem caused by the video file duration being too short?

Kyome22 avatar Aug 03 '20 01:08 Kyome22

Hey @Kyome22, apologies have been busy at work..

The video file I was using does comply with the media requirements.. I tried using a non-compliant video file (large resolution) and the error explicitly stated that the resolution was too large.

I have also tried multiple files with varying durations, however I always got the same actual duration in the error response: 67

Finally, I read somewhere in the Twitter API docs that the media_category is used to validate media used in Ads... This is why I removed that parameter which ended up working for me.

ramikalai avatar Aug 04 '20 11:08 ramikalai

Does #317 fix this issue? Or does media upload still fail?

ramikalai avatar Aug 04 '20 11:08 ramikalai