10

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 = [[FBVideoUpload 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];
}
Brenton Morse
  • 2,567
  • 4
  • 21
  • 16
  • I am not able to add this code, as in my facebook-ios-sdk FBVideoUpload class is not exist.. Can you please help me – Amit Battan Apr 05 '11 at 06:23
  • Now I am getting error `Unable to retrieve session key from the access token.` – Amit Battan Apr 05 '11 at 07:35
  • But my updates fetched ok no session error in that case – Amit Battan Apr 05 '11 at 07:43
  • I have a pretty simple fix that you can poke into one function of the current iOS Facebook SDK: http://stackoverflow.com/questions/17927767/upload-large-videos-to-facebook-with-ios-sdk/17927784#17927784 – jjxtra Jul 30 '13 at 03:28

5 Answers5

10

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
  • Thanks for the response zoul. I'll give this a shot now and see how it goes. – Brenton Morse Mar 22 '11 at 13:19
  • @zoul - I have implemented your FBVideoUpload class with the version of the Facebook IOS SDK you have with it. Although I no longer get any errors (Via NSLog I know the request does reach the didLoad method) I am getting nothing on facebook. Any thought? I will add my current code to my question. – Brenton Morse Mar 22 '11 at 17:57
  • I have just tried my app that uses the video upload code and it works. Try getting rid of the `data` parameter, it’s not needed here. Are you sure that the request succeeds? What’s the response from the server? – zoul Mar 22 '11 at 18:56
  • I'll try it without the data parameter. And I'll double check the server response and post it here once I get it. – Brenton Morse Mar 22 '11 at 19:25
  • @Brenton - Are you able to access FBVideoUpload classes ? From last day I am stuck badly in video upload to Facebook @zoul - I am using FBGraph API for different functionality in my project. But now I have to upload video too. So after long RnD I have found that have to use old Facebook API for video upload. But till date not able to access. While accessing FBVideoUpload class methods it says like session invalid. But I am passing the same session what I got from FBGraph API. I don't know whats wrong – Tariq Mar 23 '11 at 04:23
  • @Tariq - I had no problem accessing the FBVideoUpload classes. Just drag the files into your xCode project and either set your header search path or click to copy them into the project folder. Then just make sure you include facebook.h and FBVideoUpload.h. – Brenton Morse Mar 23 '11 at 13:58
  • @Brenton - Yeah I am done with that. But video is not not uploading to Facebook. I don't know whats the problem. Your problem is resolved ? – Tariq Mar 24 '11 at 04:36
  • @Tariq - Nope. Still haven't managed to get anything properly uploaded to facebook. Still working on it. – Brenton Morse Mar 24 '11 at 13:11
  • @Zoul - I've attempted everything with your code and I just can't seem to get it to work. Would you mind posting the code you have that calls your FBVideoUpload class? See if I'm doing something blatantly wrong. – Brenton Morse Mar 25 '11 at 14:05
  • I'm sorry for that. What about the server response? We might find a hint there. My uploading code is so simple that there's almost a zero chance of getting something wrong. I remember I had to fiddle with the Application Type or something like that in the Facebook developer control panel. I don't remember the exact terms, but there's a toggle between a web app and a desktop app, my code magically started working after I fiddled with that. I'll try to make a sample project, but don't hold your breath, my time schedule is very strained at the moment. – zoul Mar 25 '11 at 15:13
  • I understand, zoul. I appreciate all the help you've given so far. I've fiddle with the settings on facebook and will test this again soon (my dev iphone can't connect to office wifi for some reason, will test at home). – Brenton Morse Mar 25 '11 at 17:45
  • 1
    And it was as simple as that. Made sure my app was set to HTML/Web and not Native and it works like a charm. Thanks for all the help, zoul. – Brenton Morse Mar 26 '11 at 06:07
  • This is crazy, but I’m glad it worked. Hopefully this thread will also help others, the Facebook video upload on iOS seems to be a mess. I’ll update the comments in the GitHub code to mention the web/native toggle. – zoul Mar 26 '11 at 06:56
  • I have done these steps * I download your code https://github.com/zoul/facebook-ios-sdk . . . * take the FBVideoUpload.h/m classes from your src add add into my project . . . . * #include "FBVideoUpload.h" in FBConnect.h . . . . * then I code for upload video which Bernton posted ....... but I got error log `Unable to retrieve session key from the access token.` – Amit Battan Apr 05 '11 at 11:47
  • How should I authenticate . . . and if I change authenticate method then will it affect the other tasks??? – Amit Battan Apr 05 '11 at 12:14
  • @Amit, no offense, but *please*, don’t just spew out questions without thinking. Your comments would be better as a standalone question, because if everybody who has some trouble with video upload just dropped five or six comments here, the original points would soon be lost. The best thing you can do is delete the comments here and [ask a new question](http://stackoverflow.com/questions/ask), carefully describing your problem. – zoul Apr 05 '11 at 12:21
  • @zoul Sorry and thanks for succession ... I have put my question here http://stackoverflow.com/questions/5552556/ – Amit Battan Apr 05 '11 at 13:24
  • @Zoul thanks this is awesome. I've spent a couple of days messing round trying to get this to work using the graph api, then the rest api using the new authentication. No luck. As of May 2011 yours is the only solution I could find that works. So a huge thank you! – Max MacLeod May 11 '11 at 11:41
  • Happy to help. I think it’s possible to upload video using Graph API now, but I didn’t find the time to update the code yet. – zoul May 11 '11 at 17:03
  • @zoul When i add my Appki and login then this alert i recived "Cannot open the page because the address is invalid", Where i was wrong?thax – Umair_uas Jul 15 '11 at 08:13
  • is the branch no more available? – Satyam Mar 27 '12 at 06:07
  • @Satyamsvv, did you read the answer to the end? The branch is no more available as you can now upload videos using the official SDK. – zoul Mar 27 '12 at 06:20
  • when using official SDK, i'm getting error saying that "movie file not found". – Satyam Mar 27 '12 at 07:50
  • Sorry for a seemingly trivial question, but why are you setting a "file" key and value in the params? I can't see the need for a "file" key in FB's docs here (scroll down to Videos -> Create): http://developers.facebook.com/docs/reference/api/user/ – PostCodeism Mar 27 '12 at 22:20
  • Probably just leftover from the previous versions of the code, the sample may very well work without it. – zoul Mar 28 '12 at 05:01
  • I've found an engineering post from Facebook on uploading videos, details posted below... – PostCodeism Mar 28 '12 at 13:08
  • I had a question on uploading videos and discovered this answer: http://stackoverflow.com/questions/17927767/upload-large-videos-to-facebook-with-ios-sdk/17927784#17927784 – jjxtra Jul 30 '13 at 14:31
2
- (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]);
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
Sishu
  • 1,510
  • 1
  • 21
  • 48
2

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)

Johnmph
  • 3,391
  • 24
  • 32
  • John I take it you have been successful using the graph API to upload video? Would you be so kind as to share some sample code for this? – Max MacLeod May 10 '11 at 14:02
  • Simply use the code above with the fork mentioned, m_facebook is a Facebook object already authorized (with user_videos permission) and movieData is the raw data of the video you want to upload. – Johnmph May 11 '11 at 14:20
0

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.

Edward Ashak
  • 2,411
  • 2
  • 23
  • 38
  • Because as far as I know there is no way to extract a user's personal upload email address from facebook's graph API. If there is this would be a very simple solution. – Brenton Morse Mar 22 '11 at 14:48
0

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:

enter image description here

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...

PostCodeism
  • 1,070
  • 1
  • 12
  • 20