2

首先:看起来有两种方法可以使用 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 从我的服务器加载文件,视频出现在我的时间线上。

另一种方法是使用“source”参数通过请求直接上传视频。这应该看起来像这样:

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.

查看文档,看起来视频数据必须在“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.

那么如何将视频文件包装在表单数据中呢?

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

4

2 回答 2

1

在卡住了三天后,我终于能够得到这个错误的解决方案'您尝试上传的视频格式不受支持。请使用受支持格式的视频重试。 ' 从本地文件上传时。记住规则是不要在请求参数中包含“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)
    }

}

于 2017-10-18T17:52:59.570 回答
0

我在这个项目中找到了一个关于如何使用 FB Graph APi 执行此操作的示例: https ://github.com/brynbellomy/SEBeamMeUpScotty

相关参数为:

NSData *videoData = [NSData dataWithContentsOfURL: self.videoURL];              NSMutableDictionary *params = @{@"video.mp4":   videoData,
                                @"contentType": @"video/quicktime",
                                @"title":       @"Money Money Video Maker",
                                @"description": @"", }.mutableCopy;
于 2016-03-16T04:12:15.510 回答