0

我在 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]);


        }];



    }];

}
}];
4

1 回答 1

0

此代码已在Facebook SDK 3.14.1上成功测试

-(void)shareOnFaceBook
{
    //sample_video.mov is the name of file
    NSString *filePathOfVideo = [[NSBundle mainBundle] pathForResource:@"sample_video" ofType:@"mov"];

    NSLog(@"Path  Of Video is %@", filePathOfVideo);
    NSData *videoData = [NSData dataWithContentsOfFile:filePathOfVideo];
    //you can use dataWithContentsOfURL if you have a Url of video file
    //NSData *videoData = [NSData dataWithContentsOfURL:shareURL];
    //NSLog(@"data is :%@",videoData);
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               videoData, @"video.mov",
                               @"video/quicktime", @"contentType",
                               @"Video name ", @"name",
                               @"description of Video", @"description",
                               nil];

   if (FBSession.activeSession.isOpen)
   {
        [FBRequestConnection startWithGraphPath:@"me/videos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                              if(!error)
                              {
                                  NSLog(@"RESULT: %@", result);
                                  [self throwAlertWithTitle:@"Success" message:@"Video uploaded"];
                              }
                              else
                              {
                                  NSLog(@"ERROR: %@", error.localizedDescription);
                                  [self throwAlertWithTitle:@"Denied" message:@"Try Again"];
                              }
                          }];
    }
    else
    {
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"publish_actions",
                            nil];
        // OPEN Session!
        [FBSession openActiveSessionWithPublishPermissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone  allowLoginUI:YES
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState status,
                                                         NSError *error) {
                                         if (error)
                                         {
                                             NSLog(@"Login fail :%@",error);
                                         }
                                         else if (FB_ISSESSIONOPENWITHSTATE(status))
                                         {
                                             [FBRequestConnection startWithGraphPath:@"me/videos"
                                                                          parameters:params
                                                                          HTTPMethod:@"POST"
                                                                   completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                                                       if(!error)
                                                                       {
                                                                           [self throwAlertWithTitle:@"Success" message:@"Video uploaded"];

                                                                           NSLog(@"RESULT: %@", result);
                                                                       }
                                                                       else
                                                                       {
                                                                           [self throwAlertWithTitle:@"Denied" message:@"Try Again"];

                                                                           NSLog(@"ERROR: %@", error.localizedDescription);
                                                                       }

                                                                   }];
                                         }
                                     }];
        }
}

我得到了错误:

 The operation couldn’t be completed. (com.facebook.sdk error 5.)

它发生在 facebook 被启动时。下次我打开我的应用程序时,它运行良好,它总是第一次。尝试了应用程序中的所有内容,但它似乎在 Facebook SDK 方面。

看到的几个原因com.facebook.sdk error 5

  • 会话未打开。证实。
  • Facebook 检测到您正在向系统发送垃圾邮件。更改视频名称。
  • Facebook 使用 SDK 有明确的限制。尝试不同的应用程序。
  • 错误的发布权限。旋转publish_actions一下。
于 2014-06-28T14:00:45.443 回答