【发布时间】:2018-10-04 08:22:05
【问题描述】:
我正在尝试上传,即使用我的设备将视频分享到 Twitter。到目前为止,我已经使用下面的代码来做到这一点。
// video Upload
func requestAccessToTwitterAccount(videoURL:NSURL,fileSize:UInt32){
let accountStore = ACAccountStore()
let twitterAccountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
accountStore.requestAccessToAccounts(with: twitterAccountType, options: nil) { (granted, error) in
if granted {
let accounts = accountStore.accounts(with: twitterAccountType)
if (accounts?.count)! > 0 {
self.twitterAccount = accounts?.last as! ACAccount
self.uploadVideoToTwitter(videoURL: videoURL, fileSize: fileSize)
}
else{
let error = "Please set your twitter account in Settings."
print(error)
}
}
else {
print("App permissions are disabled in device twitter settings, please enable it.")
}
}
}
func uploadVideoToTwitter(videoURL:NSURL,fileSize: UInt32){
print(videoURL.path!)
if let videoData = NSData(contentsOfFile: videoURL.path!){
self.tweetVideoInit(videoData: videoData, videoSize: Int(fileSize))
}else{
print("Something Wrong")
}
}
func tweetVideoInit(videoData:NSData,videoSize:Int) {
let uploadURL = NSURL(string:"https://upload.twitter.com/1.1/media/upload.json")
var params = [String:String]()
params["command"] = "INIT"
params["total_bytes"] = String(videoData.length)
params["media_type"] = "video/MOV"
print(params)
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
if let err = error {
print(error!)
}else{
do {
let object = try JSONSerialization.jsonObject(with: responseData! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
if let tweetID = dictionary["media_id_string"] as? String{
self.tweetVideoApped(videoData: videoData, videoSize: videoSize, mediaId: tweetID, chunk: 0)
}
}
}
catch {
print(error)
}
}
})
}
func tweetVideoApped(videoData:NSData,videoSize:Int ,mediaId:String,chunk:NSInteger) {
let uploadURL = NSURL(string:"https://upload.twitter.com/1.1/media/upload.json")
var params = [String:String]()
params["command"] = "APPEND"
params["media_id"] = mediaId
params["segment_index"] = String(chunk)
print(params)
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.addMultipartData(videoData as Data!, withName: "media", type: "video/mov", filename:"mediaFile")
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
if let err = error {
print(err)
}else{
self.tweetVideoFinalize(mediaId: mediaId)
}
})
}
func tweetVideoFinalize(mediaId:String) {
let uploadURL = NSURL(string:"https://upload.twitter.com/1.1/media/upload.json")
var params = [String:String]()
params["command"] = "FINALIZE"
params["media_id"] = mediaId
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
if let err = error {
print(err)
}else{
do {
let object = try JSONSerialization.jsonObject(with: responseData! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
self.postStatus(mediaId: mediaId)
}
}
catch {
print(error)
}
}
})
}
func postStatus(mediaId:String) {
let uploadURL = NSURL(string:"https://api.twitter.com/1.1/statuses/update.json")
var params = [String:String]()
//params["status"] = twitterDescription
params["media_ids"] = mediaId
let postRequest = SLRequest(forServiceType: SLServiceTypeTwitter,
requestMethod: SLRequestMethod.POST,
url: uploadURL as URL!,
parameters: params)
postRequest?.account = self.twitterAccount;
postRequest?.perform(handler: { ( responseData, urlREsponse,error) in
if let err = error {
print(err)
}else{
do {
let object = try JSONSerialization.jsonObject(with: responseData! as Data, options: .allowFragments)
if let dictionary = object as? [String: AnyObject] {
print("video uploaded")
let alert = UIAlertController(title: "Success", message: "video uploaded successfully.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
catch {
print(error)
}
}
})
}
现在问题是这样的,我不确定问题出在哪里,但是当我尝试上传小于10 secs 的视频时,它已上传成功,但是当我尝试视频时超过 10 秒,它会给出 Request status 400 错误,并且视频无法上传。
注意:-我的视频格式是.MOV,大小约为6.4MB。所以我猜它是根据Twitter 的视频上传指南接受的。
仅供参考:-我也提到了这个链接 - Twitter Upload Demo
还有Official Documentation 建议以下限制,其中 我不超过。
有人可以帮我解决为什么会出现这个问题。
【问题讨论】:
-
Http 代码 400 是“错误请求”。因此,您发送的请求对 API 无效。可能缺少参数、文件大小太大等。您是否已经在 Stackoverflow 上搜索过答案?这已经被问过很多次了。
-
是的 @Scriptable 已经搜索过这个,他们建议参数是错误的,但在我的情况下,他们不是因为他们在 6-7 秒的视频中工作,但不是在 10+ 秒的视频中。 ?所以我想问题是不同的
-
所以我猜你正在达到 API 限制。如果它适用于小视频但不适用于较大的视频,那么您必须达到上传/速率限制。
-
是的,在这一点上我可能同意你的看法,但我在一些论坛上读到这似乎仅适用于 .mov 格式,大小不是问题,因为如果我上传使用 URL 的视频很容易上传,如果是 20+ 秒,那么也很容易上传。那么你能解释一下为什么会发生这种情况
-
我不是 twitter API 开发人员,也许你应该联系他们寻求支持