Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

objective c - 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 = [[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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...