You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API及RestFB发布视频至Facebook Page Story?报错求助

问题描述

尝试将视频发布到Facebook Page的Story多次失败,使用RestFB可以发布到Page的Timeline,但无法发布到Story。使用RestFB时遇到错误:"There is a problem uploading your video, Please try again with another file",已尝试多个1分钟以内的视频,问题依旧。

实现代码如下:

public void postVideo(String contentUrl) throws MalformedURLException, FileNotFoundException, FacebookOAuthException {
        Users loggedInUser = userService.getCurrentUser();
        Socials social = socialsService.getSocialByPlatform(SocialPlatform.FACEBOOK);
        UserSocials userSocial= userSocialsService.findUserSocial(loggedInUser, social).orElseThrow(() -> new UserException("User social not found"));
        FacebookClient defaultFacebookClient = new DefaultFacebookClient(userSocial.getAccessToken(), Version.LATEST);
        File videoFile = new File("uploads/" + contentUrl);
        if (!videoFile.exists()) {
            throw new FileNotFoundException("Video file not found at specified path.");
        }
        FileInputStream fileInputStream = new FileInputStream(videoFile);

//       Implementation using facebook api
//        MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();
//        requestBody.add("file", new FileSystemResource("uploads/" + contentUrl));
//        HttpHeaders headers = new HttpHeaders();
//        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
//        FBVideoStoryStartResponse fbVideoStoryStartResponse = webClient.post()
//                .uri("https://graph.facebook.com/v18.0/"+userSocial.getSocialUserId()+"/video_stories", uriBuilder -> uriBuilder
//                        .queryParam("upload_phase", "start")
//                        .build())
//                .retrieve()
//                .bodyToMono(FBVideoStoryStartResponse.class)
//                .block();
//        System.out.println(fbVideoStoryStartResponse);
//        if(fbVideoStoryStartResponse != null){
//            FBVideoStoryUploadStatus fbVideoStoryUploadStatus = webClient.post()
//                    .uri("https://rupload.facebook.com/video-upload/v18.0/" + fbVideoStoryStartResponse.getVideoId())
//                    .headers(httpHeaders -> httpHeaders.addAll(headers)) // Add the headers to the request
//                    .bodyValue(requestBody)
//                    .retrieve()
//                    .bodyToMono(FBVideoStoryUploadStatus.class)
//                    .block();
//        System.out.println(fbVideoStoryUploadStatus);
//            assert fbVideoStoryUploadStatus != null;
//            if(fbVideoStoryUploadStatus.isSuccess()){
//                FBVideoStoryFinishResponse fbVideoStoryFinishResponse = webClient.post()
//                        .uri("https://graph.facebook.com/v18.0/"+userSocial.getSocialUserId()+"/video_stories", uriBuilder -> uriBuilder
//                                .queryParam("video_id", fbVideoStoryStartResponse.getVideoId())
//                                .queryParam("upload_phase", "finish")
//                                .build())
//                        .retrieve()
//                        .bodyToMono(FBVideoStoryFinishResponse.class)
//                        .block();
//                System.out.println(fbVideoStoryFinishResponse);
//            }
//        }


//      Implementation using restfb
        FBVideoStoryStartResponse fbVideoStoryStartResponse1 = defaultFacebookClient.publish(userSocial.getSocialUserId() + "/video_stories", FBVideoStoryStartResponse.class,
                Parameter.with("upload_phase", "start"));
        String videoUploadID = fbVideoStoryStartResponse1.getVideoId();
        System.out.println(videoUploadID);
        GraphResponse graphResponse = defaultFacebookClient.publish(videoUploadID, GraphResponse.class, BinaryAttachment.with(videoFile.getName(), fileInputStream));
        GraphResponse graphResponse1 = defaultFacebookClient.publish(userSocial.getSocialUserId() + "/video_stories", GraphResponse.class,
                Parameter.with("video_id", videoUploadID), 
                Parameter.with("upload_phase", "finish"), 
                Parameter.with("video_state", "PUBLISHED"),
                Parameter.with("description", "A short description text"));
            try {
                fileInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
}
排查与解决建议
  • 校验视频格式合规性
    Facebook Story对视频有明确要求:

    • 编码:H.264视频编码 + AAC音频编码
    • 分辨率:9:16竖屏,720x1280至1080x1920之间
    • 文件大小:不超过4GB(建议控制在100MB以内)
    • 帧率:30fps以内
      用FFmpeg转换视频到合规格式,示例命令:
    ffmpeg -i input.mp4 -c:v libx264 -c:a aac -profile:v baseline -level 3.0 -pix_fmt yuv420p -vf "scale=1080:1920" output_story.mp4
    
  • 修正RestFB上传逻辑

    • 上传视频时需指定MIME类型,当前BinaryAttachment未设置,改为:
      BinaryAttachment.with(videoFile.getName(), fileInputStream, "video/mp4")
      
    • 确认userSocial.getSocialUserId()是Page的ID而非用户个人ID,发布Page Story必须使用Page ID,且AccessToken需为Page令牌(包含pages_manage_posts、pages_show_list权限)。
  • 检查权限与API版本

    • 确保AccessToken拥有pages_manage_stories、pages_manage_posts权限,且权限未过期
    • 锁定RestFB的API版本(如Version.V18_0),避免Version.LATEST带来的兼容性问题
  • 添加详细错误日志
    在上传步骤捕获完整的Facebook异常信息,便于定位问题:

    try {
        GraphResponse graphResponse = defaultFacebookClient.publish(videoUploadID, GraphResponse.class, BinaryAttachment.with(videoFile.getName(), fileInputStream, "video/mp4"));
    } catch (FacebookOAuthException e) {
        System.err.println("OAuth错误信息: " + e.getMessage());
        System.err.println("错误代码: " + e.getErrorCode());
        throw e;
    } catch (FacebookException e) {
        System.err.println("Facebook通用错误: " + e.getMessage());
        throw e;
    }
    

内容的提问来源于stack exchange,提问作者David Ogbodu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 04:14:55