mattdonnelly / Swifter

[DEPRECATED] :bird: A Twitter framework for iOS & OS X written in Swift
MIT License
2.37k stars 387 forks source link

Post Tweet with Media #313

Open ramikalai opened 4 years ago

ramikalai commented 4 years ago

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

Kyome22 commented 4 years ago

@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 commented 4 years ago

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

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

meteochu commented 4 years ago

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

ramikalai commented 4 years ago

@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 commented 4 years ago

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 !

Kyome22 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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?

ramikalai commented 4 years ago

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 commented 4 years ago

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