【问题标题】:Uploading video to Facebook with Facebook iOS SDK 3.x使用 Facebook iOS SDK 3.x 将视频上传到 Facebook
【发布时间】:2012-08-24 14:01:27
【问题描述】:

我找到了很多使用 facebook SDK 上传视频的解决方案,但它们都不起作用(包括来自 facebook 开发者博客的信息)。这是我用来上传视频的代码。 “out_composed.mov”已成功创建,因为我可以将该视频保存在图库中或通过电子邮件发送。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSString* composedOutputPath = [documentsDirectoryPath stringByAppendingPathComponent:@"out_composed.mov"];
NSData *videoData = [NSData dataWithContentsOfFile:composedOutputPath];

NSMutableDictionary<FBGraphObject>* params = [FBGraphObject graphObject];
[params setDictionary:[NSMutableDictionary dictionaryWithObjectsAndKeys:
                       videoData, @"video.mov",
                       @"video/quicktime", @"contentType",
                       @"My awesome video", @"title", nil]];

if (!appDelegate.session.isOpen)
{
    [FBSession openActiveSessionWithPermissions:[NSArray arrayWithObjects:@"offline_access", @"publish_stream",@"user_videos",@"video_upload",nil] allowLoginUI:true completionHandler:^(FBSession *session, FBSessionState status, NSError *error)
                           {
                               FBRequest* uploadVideoRequest = [FBRequest requestForPostWithGraphPath:@"/me/videos" graphObject:params];
                               [uploadVideoRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
                                {
                                    // The server returns "353 missing video file" here.
                                    if(error != nil) NSLog(@"%@",[error description]);
                                    else NSLog(@"Error: %@", error);
                                }];
                           }];
    appDelegate.session = [FBSession activeSession];
} else
{
    FBRequest* uploadVideoRequest = [FBRequest requestForPostWithGraphPath:@"/me/videos" graphObject:params];
    [uploadVideoRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error)
     {
         if(error != nil) NSLog(@"%@",[error description]);
         else NSLog(@"Error: %@", error);
     }];
}

我得到的错误是:

Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x1fdc94a0 {com.facebook.sdk:ParsedJSONResponseKey={
    body =     {
        error =         {
            code = 353;
            message = "(#353) Missing video file";
            type = OAuthException;
        };
    };
    code = 400;
}, com.facebook.sdk:HTTPStatusCode=400}

我该如何解决这个问题?提前非常感谢!

海子

【问题讨论】:

  • 在将视频附加到请求之前,您是否尝试过对视频进行 mime 编码? (不过,这只是在黑暗中刺伤)
  • 我正在使用 AVAssetExportSession 导出合成电影。我假设编码由 AVAssetExportSession 或 AVAssetWriter 处理。如果我保存或通过电子邮件发送它就可以正常播放。
  • 您确定 videoData 包含数据吗? “缺少视频文件”可能是相关的。
  • 是的,我的导出器中有一个回调,它会通知它的代理导出完成。当我使用完全相同的文件保存到 iOS 画廊时,它可以工作。使用此文件创建电子邮件有效。只有在尝试构建 Facebook 上传时,才会失败。
  • 我读到,上传视频应该使用graph-video.facebook... URL,但没有明显的方法。

标签: ios facebook-graph-api video


【解决方案1】:

以下对我使用 3.0.8 SDK 有效(使用 -requestWithGraphPath:... 而不是 -requestForPostWithGraphPath:...):

NSDictionary *parameters = [NSDictionary dictionaryWithObject:videoData forKey:@"sample.mov"];
FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos" parameters:parameters HTTPMethod:@"POST"];
[request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
    NSLog(@"result: %@, error: %@", result, error);
}];

似乎缺少的是一种显示上传进度的方法。

【讨论】:

  • 我将您的答案标记为正确。也在这里工作。奇怪的。也许我没有在 3.0.8 上尝试这个变体,而是在 3.0.6b 上尝试。谢谢分享。
猜你喜欢
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-18
  • 2012-03-04
相关资源
最近更新 更多