ios - 使用 iOS FBSDK v4 (multipart/form-data) 上传到 facebook 时编码视频的正确方法

标签 ios facebook swift facebook-graph-api fbsdk

首先:看起来有两种方法可以使用 GraphRequest 将视频上传到 facebook。一个使用“file_url”参数的。您提供一个 URL,Facebook 从外部服务器下载视频文件。像这样效果很好:

let params = [
    "title": "...",
    "description": "...",
    "file_url": "http://example.com/videofile.mp4"
]

let rq = FBSDKGraphRequest(graphPath: "me/videos", parameters: params, HTTPMethod: "POST")

rq.startWithCompletionHandler { (conn, result, error) -> Void in
    // handle error etc..
}

Facebook 从我的服务器加载文件,视频出现在我的时间线上。

另一种方法是使用“源”参数直接通过请求上传视频。那应该有点像这样:

let videoURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("testvideo", ofType: "mp4")!)
let rawData = NSData(contentsOfURL: videoURL)!

let params = [
    "title": "...",
    "description": "...",
    "source": rawData
]

然而,此错误消息失败:

The video you're trying to upload is in a format that isn't supported. Please try again with a video in a supported format.

看看 documentation看起来视频数据必须以“multipart/form-data”编码。该文档甚至提供了指向 w3 的链接。 .

然而,如何做到这一点对我来说仍然是个谜。我尝试了很多组合来制作带有二进制视频数据的表单数据字符串,例如:

var s = ""
s += "Content-Type: multipart/form-data; boundary=XXXXXXXXXXXXXXXXXXXX\r\n\r\n"
s += "--XXXXXXXXXXXXXXXXXXXX\r\n"
s += "Content-Type: application/octet-stream\r\n\r\n"

let p = "--XXXXXXXXXXXXXXXXXXXX--"

let data = NSMutableData()
data.appendData(s.asUTF8Data())
data.appendData(rawData)
data.appendData(p.asUTF8Data())

let params = [
    "title": "...",
    "description": "...",
    "source": data
]

和其他组合

s += "Content-Transfer-Encoding: binary\r\n"
s += "Content-Disposition: file; filename=\"video.mp4\"\r\n"

但没有成功。我总是收到相同的错误消息:

The video you're trying to upload is in a format that isn't supported. Please try again with a video in a supported format.

那么如何将一个视频文件包裹在form-data中呢?

以防万一: 不,很遗憾,我不能使用 FBSDK v4 中已经提供的视频共享功能。

最佳答案

卡住了三天后,我终于找到了这个错误的解决方案“您尝试上传的视频格式不受支持。请使用受支持格式的视频重试。 ' 从本地文件上传时。请记住,规则是不要在请求参数中包含“source”或“file_url”,即使 FB 文档这么说也是如此。 下面是我的工作代码:

func uploadVideoToWall() {
    var videoData: NSData
    guard let localVideoPath = Bundle.main.path(forResource: "overthinking", ofType: "mp4") else {
        print("local video not found")
        return
    }
    print("local video path: \(localVideoPath)")
    let localPathURL = URL(fileURLWithPath: localVideoPath, isDirectory: false)
    print("absolute string: \(localPathURL.absoluteString)")

    do {
        videoData = try NSData(contentsOf: localPathURL)
        var strDesc : String
        strDesc = "🤡🤡🤡 testing upload from test app 2"
        //Do no include "source" or "file_url" in the request parameters even though the FB documentation says so.
        let videoObject: [String : Any] = ["contentType": "multipart/form-data", "title": "Testing yoooo", "description": strDesc, localPathURL.absoluteString: videoData]
        //self.view!.isUserInteractionEnabled = false

        let uploadVideoRequest = FBSDKGraphRequest(graphPath: "me/videos", parameters: videoObject, httpMethod: "POST")
        let connection = FBSDKGraphRequestConnection()
        connection.delegate = self
        connection.add(uploadVideoRequest, completionHandler: { (connection, result, error) in
            if error != nil {
                print("Error: \(String(describing: error))")

            }
            else {
                print("Success")
            }
        })
        connection.start()

    } catch {
        print(error)
    }

关于ios - 使用 iOS FBSDK v4 (multipart/form-data) 上传到 facebook 时编码视频的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688749/

相关文章:

ios - Xamarin.iOS 应用程序在 iOS 13 上卡住

ios - Swift 中的无限 UIStepper

ios - 通过 xcodebuild 命令更改目标属性

ios - RxSwift - 类属性的绑定(bind)

swift - 二元运算符问题 '-'

ios - 如何在不耗尽电池的情况下每 5 秒或当用户移动阈值时获得高精度位置?

ios - iOS 版 Yandex map

ios - 如何在降压构建上运行 xcconfig

php - Facebook SDK v4 和 Graph Api 2.x 在粉丝页面墙中自动发布

swift - 有什么方法可以快速运行单元测试而无需实际运行应用程序