xcode iPhone Facebook 视频上传
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5355846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone Facebook Video Upload
提问by Brenton Morse
I've been working on this for a couple of days now and just can't seem to find a straight answer or example anywhere. I am trying to upload a video to facebook from within my iPhone App. I can connect to facebook (and have uploaded pictures) without a problem using:
我已经为此工作了几天,似乎无法在任何地方找到直接的答案或示例。我正在尝试从我的 iPhone 应用程序中将视频上传到 Facebook。我可以使用以下方法毫无问题地连接到 facebook(并上传了图片):
_facebook = [[Facebook alloc] initWithAppId:kAppID];
_permissions = [[NSArray arrayWithObjects:@"publish_stream", @"offline_access",nil] retain];
[_facebook authorize:_permissions delegate:self];
However I can't seem to get my video uploading working. My current code is:
但是我似乎无法让我的视频上传工作。我目前的代码是:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"TestMovie" ofType:@"mp4"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
data, @"video",
nil, @"callback",
@"test", @"title",
@"upload testing", @"description",
@"EVERYONE", @"privacy",
nil];
[_facebook requestWithMethodName:@"video.upload"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
And since video upload calls have to be made to a different server I changed the restserver url within the facebook.m file to:
由于视频上传调用必须发送到不同的服务器,我将 facebook.m 文件中的 restserver url 更改为:
static NSString* kRestserverBaseURL = @"https://api-video.facebook.com/method/";
When I run this the upload crashes with an error:
当我运行它时,上传崩溃并出现错误:
facebookErrDomain err 353.
Any help would be appreciated.
任何帮助,将不胜感激。
EDIT:
编辑:
With Zoul's help I now have the following code implemented (I have done nothing to alter his upload class nor the version of the SDK it came with). The request no longer gets an error however nothing is being uploaded.
在 Zoul 的帮助下,我现在实现了以下代码(我没有做任何更改他的上传类及其附带的 SDK 版本)。请求不再出错,但没有上传任何内容。
I initialize the facebook object and the upload object:
我初始化 facebook 对象和上传对象:
_facebook = [[Facebook alloc] initWithAppId:kAppID];
_permissions = [NSArray arrayWithObjects:@"publish_stream", @"offline_access",nil];
[_facebook authorize:_permissions delegate:self];
_upload = [[FBVideoUpload alloc] init];
And then I use it once facebook has logged in:
然后我在 facebook 登录后使用它:
- (void)fbDidLogin{
_upload.accessToken = _facebook.accessToken;
_upload.apiKey = kApiKey;
_upload.appSecret = kApiSecret;
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Test" ofType:@"mp4"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
data, @"",
@"test", @"title",
@"upload testing", @"description",
@"EVERYONE", @"privacy",
nil];
[_upload startUploadWithURL:fileURL params:params delegate:self];
}
采纳答案by zoul
I've got a video upload branchin my fork of the Facebook SDK on GitHub. I did not touch it for several weeks, but it used to work fine (only it requires the old-style authentication, see this branch). There are some comments in the FBVideoUpload class header, but the interface is pretty much self-explanatory. There's also some helpful discussion under my pull request– especially the thing about SSL certificates on the api-video
cluster that could make the whole issue easier, but I did not review the code yet.
我在 GitHub 上的 Facebook SDK分支中有一个视频上传分支。我有几个星期没有碰它,但它曾经工作得很好(只有它需要旧式身份验证,请参阅此分支)。FBVideoUpload 类标头中有一些注释,但界面几乎一目了然。在我的pull request下还有一些有用的讨论——尤其是关于api-video
集群上的 SSL 证书的事情,它可以使整个问题更容易,但我还没有代码。
[Rant: It's a pity that the Facebook SDK for iOS does not exactly thrive on GitHub. There are many pull requests, but the official developers never seem to merge anything, not even trivial typo fixes in the documentation. Most of the time the pull requests simply sit there until rejected.]
[Rant:遗憾的是,iOS 版 Facebook SDK 并没有完全在 GitHub 上蓬勃发展。有很多拉取请求,但官方开发人员似乎从来没有合并任何东西,甚至没有在文档中进行微不足道的错字修复。大多数情况下,拉取请求只是坐在那里直到被拒绝。]
And yes, did I mention that the video upload code is a messy hack? The video upload code is a messy hack. It parses some auth tokens and it could break anytime soon, but it was the only way I could make it work back then.
是的,我有没有提到视频上传代码是一个凌乱的黑客?视频上传代码是一个凌乱的黑客。它解析一些身份验证令牌并且它可能很快就会中断,但这是我当时让它工作的唯一方法。
Update:The video upload branch is no more, you can now easily upload video using the official SDK:
更新:视频上传分支不再存在,您现在可以使用官方SDK轻松上传视频:
NSData *videoData = [NSData dataWithContentsOfURL:movieURL];
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
…, @"title",
…, @"description",
…, @"file",
videoData, @"clip.mov",
nil];
[facebook requestWithGraphPath:@"me/videos" andParams:params andHttpMethod:@"POST" andDelegate:self];
This is “the right way to do it”?, the previous solution was just a temporary hack.
这是“正确的做法”吗?,以前的解决方案只是一个临时的黑客。
回答by Johnmph
You can use new graph api to upload video :
您可以使用新的图形 API 上传视频:
- Use me/videos as graph path
- Add user_videos permission to your app
- Set filename to match your file's extension in the POST request (after Content-Disposition:)
- 使用我/视频作为图形路径
- 为您的应用添加 user_videos 权限
- 设置文件名以匹配 POST 请求中的文件扩展名(在 Content-Disposition 之后:)
You can use this fork https://github.com/johnmph/facebook-ios-sdkand have a param dictionary like that :
你可以使用这个 fork https://github.com/johnmph/facebook-ios-sdk并有一个像这样的参数字典:
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:movieData, @"source", @"File.mov", @"filename", nil];
[m_facebook requestWithGraphPath:@"me/videos" andParams:params andHttpMethod:@"POST" andDelegate:self];
I added also to this fork a method to track the upload status (by example to use with a progress bar)
我还在这个 fork 中添加了一种跟踪上传状态的方法(通过示例与进度条一起使用)
回答by Alok SInha
- (void)viewDidLoad
{
facebook = [[Facebook alloc] initWithAppId:@"276907032339239"];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonClicked:(id)sender
{
NSArray* permissions = [[NSArray alloc] initWithObjects:
@"publish_stream", nil];
[facebook authorize:permissions delegate:self];
[permissions release];
}
- (void)fbDidLogin
{
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"mov"];
NSData *videoData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, @"video.mov",
@"video/quicktime", @"contentType",
@"Video Test Title", @"title",
@"Video Test Description", @"description",
nil];
[facebook requestWithGraphPath:@"me/videos"
andParams:params
andHttpMethod:@"POST"
andDelegate:self];
NSLog(@"hiiiiiiiii");
}
-(void)fbDidNotLogin:(BOOL)cancelled
{
NSLog(@"did not login");
}
- (void)request:(FBRequest *)request didLoad:(id)result
{
if ([result isKindOfClass:[NSArray class]])
{
result = [result objectAtIndex:0];
}
NSLog(@"Result of API call: %@", result);
}
- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
NSLog(@"Failed with error: %@", [error localizedDescription]);
}
回答by PostCodeism
Actually all, it's worth mentioning to keep an eye out on Facebook's various engineering blogs. This one is dated from October 2011, so doesn't have the "current" SDK: http://developers.facebook.com/blog/post/532/
实际上,值得一提的是关注 Facebook 的各种工程博客。这个是 2011 年 10 月的,所以没有“当前”的 SDK:http: //developers.facebook.com/blog/post/532/
But the sample still works if you modify the bundle ID, add the URL scheme to the plist, and add your own App ID to the view controller:
但是,如果您修改 bundle ID,将 URL 方案添加到 plist,并将您自己的 App ID 添加到视图控制器,则示例仍然有效:
This is the most useful information i've found on video uploading - I don't know why Facebook obscure this away from their main SDK docs! It's bizarrely not linked anywhere, i had to do some digging around...
这是我在视频上传中找到的最有用的信息 - 我不知道为什么 Facebook 把它从他们的主要 SDK 文档中隐藏起来!奇怪的是,它在任何地方都没有链接,我不得不四处挖掘……
回答by Edward Ashak
Did you try sending it to an email as facebook sugested in http://www.facebook.com/help/?faq=12345You could use the email subject to set the caption of the video.
您是否尝试将其发送到电子邮件作为 facebook sugested 在http://www.facebook.com/help/?faq=12345您可以使用电子邮件主题来设置视频的标题。