iOS 6.3 アプリ内で Facebook SDK 3.6 を使用して、ビデオを Facebook にアップロードしています。
これに関する多くのスタック オーバーフローの投稿を見てきましたが、それらはすべて何年も前のものであり、はるかに古い 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]);
}];
}];
}
}];