Reputation: 108
i have been trying to publish videos on a facebook page's story, but i havent been successful, i have tried using their apis and integrating with restfb as well, As for restfb, i was able to post on a facebook page's timeline, but not on their story, i have included the code showing my implementation using the facebook api and rest fb, thanks for checking out, i appreciate any assistance
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);
}
}
So using restfb, i get the error "There is a problem uploading your video, Please try again with another file" and i have tried with multiple files below a minute but still the same result.
Upvotes: -1
Views: 112
Reputation: 1462
Here is an example code, using a byte array. Using the input stream works almost the same.
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());
Upvotes: 0