iPhone Facebook Video Upload
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:
_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:
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.
I initialize the facebook object and the upload object:
_facebook = [[Facebook alloc] initWithAppId:kAppID];
_permissions = [NSArray arrayWithObjects:@"publish_stream", @"offline_access",nil];
[_facebook authorize:_permissions delegate:self];
_upload = [[FBVideoUp开发者_开发知识库load alloc] init];
And then I use it once facebook has logged in:
- (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];
}
I’ve got a video upload branch in 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.
[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.]
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:
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.
You can use new graph api to upload video :
- 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:)
You can use this fork https://github.com/johnmph/facebook-ios-sdk and have a param dictionary like that :
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)
- (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]);
}
Did you try sending it to an email as facebook sugested in http://www.facebook.com/help/?faq=12345 You could use the email subject to set the caption of the video.
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/
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:
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...
精彩评论