我在 iOS 6.3 应用程序中使用 Facebook SDK 3.6 将视频上传到 Facebook。
我已经查看了许多关于此的 Stack Overflow 帖子,但它们都已有多年历史,并且使用的是更旧的 Facebook SDK。
有时它可以工作,有时它会失败并显示以下消息:
unexpected error:Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x1e2affc0 {com.facebook.sdk:HTTPStatusCode=500, com.facebook.sdk:ParsedJSONResponseKey={
body = {
"error_code" = 1;
"error_msg" = "An unknown error occurred";
};
code = 500;
}, com.facebook.sdk:ErrorSessionKey=, expirationDate: 4001-01-01 00:00:00 +0000, refreshDate: 2013-07-30 10:54:22 +0000, attemptedRefreshDate: 0001-12-30 00:00:00 +0000, permissions:(
"publish_stream"
)>}
这是我的代码:
FBRequestConnection *_currentConnection;
[FBSession.activeSession requestNewPublishPermissions:@[@"publish_stream"]
defaultAudience:FBSessionDefaultAudienceOnlyMe
completionHandler:^(FBSession *session, NSError *error) {
if (!error) {
NSError *attributesError;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:url.path error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
NSLog(@"file size: %lld", fileSize);
NSString *filename = [url lastPathComponent];
NSLog(@"filename: %@", filename);
NSString *mimeType = [self MIMETypeForFilename:filename
defaultMIMEType:@"video/mp4"];
NSLog(@"mime type: %@", mimeType);
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
videoData, filename,
mimeType, @"contentType",
self.song.name, @"title",
_videoDescription, @"description",
nil];
FBRequest *request = [FBRequest requestWithGraphPath:@"me/videos"
parameters:params
HTTPMethod:@"POST"];
_currentConnection = [request startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
self.stageLabel.text = @"";
NSLog(@"result: %@, error: %@", result, error);
if(error) {
// Facebook SDK * error handling *
// if the operation is not user cancelled
if (error.fberrorCategory != FBErrorCategoryUserCancelled) {
[self showAlert:@"Video Post" result:result error:error];
}
self.uploadBarButtonItem.enabled = YES;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Video Uploaded" message:@"Video has been uploaded"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[self.delegate facebookUploaderUploadSucceeded:self];
}
// Delete the temp video
NSError *err;
[[NSFileManager defaultManager] removeItemAtURL:_sourceURL error:&err];
NSLog(@"Deleting video %@: %@", _sourceURL, [err localizedDescription]);
}];
}];
}
}];