upload video to facebook using facebook sdk 3.1 on ios 6.0

10,346

Solution 1

Context

Before you can publish onto Facebook, you must get publish (write) permissions, using either native integration or the Facebook SDK, the rule is you must first acquire read permissions before write permissions.

Thus, make sure that before you attempt to upload a video, you should have requested basic info (email for example), then, once you have this, you can request write permissions. The permission necessary for uploading videos is publish_stream.

Using iOS 6 native Facebook integration

Using the native iOS 6 Facebook integration, you should use the requestForServiceType:requestMethod:URL:parameters: method of SLRequest, as follows:

- (void)upload{
    NSURL *videourl = [NSURL URLWithString:@"https://graph.facebook.com/me/videos"];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
    NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
    NSData *videoData = [NSData dataWithContentsOfFile:filePath];

    NSDictionary *params = @{
                            @"title": @"Me being silly",
                            @"description": @"Me testing the video upload to Facebook with the new system."
                            };

    SLRequest *uploadRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                  requestMethod:SLRequestMethodPOST
                                                            URL:videourl
                                                     parameters:params];
    [uploadRequest addMultipartData:videoData
                           withName:@"source"
                               type:@"video/quicktime"
                           filename:[pathURL absoluteString]];

    uploadRequest.account = self.facebookAccount;

    [uploadRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
        if(error){
            NSLog(@"Error %@", error.localizedDescription);
        }else
            NSLog(@"%@", responseString);
    }];
}

Here it's important to note that the video data does not go into the parameters dictionary, it must be added to the SLRequest object using the addMultipartData:withName:type:filename: method.

Also note that the filename is very important when adding the videos data. Here I am just using the full path of the file.

Using Facebook SDK 3.1 library

If you must support iOS versions earlier then iOS 6 or you wish to use the Facebook SDK 3.1 for any other reason, uploading a video is a little different.

You must use a FBRequest object and a NSDictionary that contains the videos details. The method I recommend using is requestWithGraphPath:parameters:HTTPMethod:, I've used this method out of preference although you should be able to use some of the other methods to create your request object.

The following code works with Facebook SDK 3.1 to upload a video:

- (void)upload{
    if (FBSession.activeSession.isOpen) {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
        NSURL *pathURL = [[NSURL alloc]initFileURLWithPath:filePath isDirectory:NO];
        NSData *videoData = [NSData dataWithContentsOfFile:filePath];

        NSDictionary *videoObject = @{
                                      @"title": @"FB SDK 3.1", 
                                      @"description": @"hello there !", 
                                      [pathURL absoluteString]: videoData
                                     };
        FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos"
                                                        parameters:videoObject
                                                        HTTPMethod:@"POST"];

        [uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
            if (!error)
                NSLog(@"Done: %@", result);
            else
                NSLog(@"Error: %@", error.localizedDescription);
        }];
    }
}

Here as you can see, we add the videos data into the parameters dictionary unlike the previous solution, it's there along with title and description (which are 2 optional parameters). Also note that here there is no key source, as specified by the Facebook documentation. The key's name is the filename of the video. I don't know why this shouldn't be source, but using source results in a com.facebook.sdk error 5.

The bug I mentioned I filed with Facebook, you can see this report on this link - unless I'm mistaken in my interpretation of the documentation. Please try out that bug and report if you can reproduce it. Thanks !

Solution 2

publish_stream is not enough to upload(read), you need to ask for "video_upload" permission.

Share:
10,346
arachide
Author by

arachide

Updated on June 25, 2022

Comments

  • arachide
    arachide almost 2 years

    One of my app is to upload video to facebook account. I checked on web, but found that most of solution are old or removed. Is there any updated solution?

    Welcome any comment

  • arachide
    arachide over 11 years
    I tested both methods on ios6, and 4,5 but they(facebook sample [hackbook] ) never worked for video upload, - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response never be triggered
  • Daniel
    Daniel over 11 years
    You are not using Facebook iOS SDK 3.1. It's a new framework from Facebook. You need to get it from the Facebook developer site and it needs to be installed onto your mac, also you must make sure you set it up correctly. Follow the instructions on the developer site.
  • arachide
    arachide over 11 years
    I have updated the question, I used the hackbook sample in fackbook ios sdk, upload photo is OK, but for video upload, - (void)request:(FBRequest *)request didReceiveResponse never triggered and reported http status error 400 finally, I change nothing of Facebook sample code, I work on mountain lion, xcode 4.5, iOS 6 base sdk, iOS 6 simulator
  • Daniel
    Daniel over 11 years
    I just checked out the sample project provided by Facebook in the SDK, and the one you're using (I didn't check the others), it's targeting iOS 4.0 and is using FBConnect. It includes the new Facebook SDK framework but it's not using it. So it looks like Facebook didn't update their Sample projects for the new SDK. I did a sample project myself, check it here: bitbucket.org/danielphillips/fb-video-upload
  • Daniel
    Daniel over 11 years
    You need to "get me" before you upload (read permissions before write permissions)
  • Wish
    Wish almost 11 years
    Can we get list of wall posts from face book...because I want pictures and videos in my application. OR Can we get any videos from face book in iPhone application.
  • Atif Imran
    Atif Imran over 10 years
    @Daniel You have great answer here however I'm stuck at one place. You're picking the video from filesystem, how about picking it from the iPhone's photo album or using camera? what should go in the line: NSString *filePath = [[NSBundle mainBundle] pathForResource:@"me" ofType:@"mov"];
  • Darshan Kunjadiya
    Darshan Kunjadiya over 9 years
    @Daniel please tell me how i add in this line "self.facebookAccount" ? what i add in this line ? please tell me .
  • Milan patel
    Milan patel over 9 years
    @Daniel please tell me what do you mean of ..self.facebookAccount .where i can define.?
  • Mohd Sadham
    Mohd Sadham about 9 years
    @Daniel How do i know the opened session having permission for public_stream?
  • Vaibhav Jhaveri
    Vaibhav Jhaveri almost 9 years
    Is it possible to add Video Data to share on Twitter ?