如何将视频发布到 Facebook 页面的故事?

问题描述 投票:0回答:1

我一直在尝试在Facebook页面的故事上发布视频,但我没有成功,我尝试过使用他们的API并与restfb集成,至于restfb,我能够在Facebook页面的时间轴上发布,但不能在他们的故事中,我已经包含了使用 facebook api 和rest fb 显示我的实现的代码,感谢您的查看,我感谢任何帮助

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);
            }
}

因此,使用restfb,我收到错误“上传您的视频时出现问题,请使用另一个文件重试”,我已在一分钟内尝试了多个文件,但结果仍然相同。

java spring-boot facebook facebook-graph-api restfb
1个回答
0
投票

这是一个使用字节数组的示例代码。使用输入流的工作原理几乎相同。

byte[] videoAsBytes = fetchBytesOfVideoYouLikeToPublish();
String pageAccessToken = "<some page access token>";
String pageId = "<the page id>";

// define client
FacebookClient fbClient = new DefaultFacebookClient(pageAccessToken, 
     Version.LATEST);

// create video upload container and save the video id
ResumableUploadStartResponse videoResponse = fbClient.publish(pageId + "/video_stories",
  ResumableUploadStartResponse.class, Parameter.with("upload_phase","start"));
String videoId = videoResponse.getVideoId();

// upload the video to the container. you need the Reel Attachment, because of the special upload mechanism
GraphResponse uploadResponse =
    fbClient.publish(videoId, GraphResponse.class, 
FacebookReelAttachment.withByteContent(videoAsBytes));

// close the container and publish the video as story
GraphResponse publishResponse = fbClient.publish(pageId + "/video_stories",
  GraphResponse.class, Parameter.with("video_id", videoId), 
Parameter.with("upload_phase", "finish"));

System.out.println("Post ID: " + publishResponse.getPostId());
© www.soinside.com 2019 - 2024. All rights reserved.