在5月1日之前,我可以使用RestFB上传视频,但是现在如何使用RestFB 2.8.0上传视频?

时间:2018-08-13 07:19:38

标签: java facebook restfb

1,获取authorizeURL

FacebookClient client = new DefaultFacebookClient(Version.LATEST);
        ScopeBuilder scope = new ScopeBuilder();
        scope.addPermission(FacebookPermissions.PUBLISH_ACTIONS);
        scope.addPermission(FacebookPermissions.USER_VIDEOS);
        authorizeURL = client.getLoginDialogUrl(appId, 
                redirectUri + "?partnerId=" + partnerId, 
                scope, Parameter.with("auth_type", "rerequest"));

2,从1)获取“代码”,然后获取访问令牌

com.restfb.FacebookClient.AccessToken accessToken = new DefaultFacebookClient(Version.LATEST).
                obtainUserAccessToken(appId, appSecret, redirectUri, code);

3,上传视频

File videoFile = new File(filePath);
long filesizeInBytes = videoFile.length();
FileInputStream in = new FileInputStream(videoFile);
FacebookClient facebook = new DefaultFacebookClient(accessToken, Version.LATEST);

ResumableUploadStartResponse returnValue = facebook.publish("me/videos",
                            ResumableUploadStartResponse.class, 
                            Parameter.with("upload_phase", "start"), 
                            Parameter.with("file_size", filesizeInBytes)); 

long startOffset = returnValue.getStartOffset();
long endOffset = returnValue.getEndOffset();
String vidId = returnValue.getVideoId();
int length = (int) (endOffset - startOffset);

String uploadSessionId = returnValue.getUploadSessionId();
while (length > 0) 
{
     byte fileBytes[] = new byte[length];
     in.read(fileBytes);

     ResumableUploadTransferResponse filePart = facebook.publish("me/videos",
          ResumableUploadTransferResponse.class,
          BinaryAttachment.with("video_file_chunk", fileBytes),
          Parameter.with("upload_phase", "transfer"),
          Parameter.with("start_offset", startOffset),
          Parameter.with("upload_session_id", uploadSessionId)
     );

    startOffset = filePart.getStartOffset();
    endOffset = filePart.getEndOffset();
    length = (int) (endOffset - startOffset);
}

GraphResponse finishResponse = facebook.publish("me/videos",
                GraphResponse.class,
                Parameter.with("upload_phase", "finish"), 
                Parameter.with("upload_session_id", uploadSessionId),
                Parameter.with("title", title),
                Parameter.with("description", "test upload video"),
                Parameter.with("privacy", "{\"value\":\"EVERYONE\"}"));

但是现在,获取访问令牌失败,并且上传视频失败。 如何使用RestFB 2.8.0将视频上传到Facebook?

0 个答案:

没有答案