我正在努力上传 Facebook 视频。获取令牌后实现代码时遇到的错误是 400 bad request。
我能够登录并获得令牌。我为 facebook 应用程序设置了允许视频上传的权限,并检查了这些权限是否正确。我也可以使用相同的方法发布图像,但由于某种原因,发布视频失败并出现 400 错误。
下面的代码来自 Unity3d。下面的代码基本上是做一个 POST 并将视频数据添加到帖子中。就像我说的那样,它适用于图像文件,但不适用于视频。
private WWW w;
public void FBPostVideo(){
Debug.Log("in the fbpostvideo ienumerator");
var moviebytes = System.IO.File.ReadAllBytes(Application.persistentDataPath+"/screenrecording.mp4");
Debug.Log(moviebytes.Length);
WWWForm form = new WWWForm();
var video_desc="walkingthedog";
var video_title="dogwalk";
var access_token=token;
form.AddBinaryData("file", moviebytes, "myvideo");
var formurl="https://graph-video.facebook.com/me/videos?title="+video_title
+ "&description=" +video_desc + "&"+token;
Debug.Log(formurl);
WWW w = new WWW(formurl, form);
StartCoroutine(waitForMovie(w));
}
public IEnumerator waitForMovie(WWW w){
yield return w;
if (!String.IsNullOrEmpty(w.error))
Debug.Log(w.error);
else
Debug.Log("Finished Uploading");
}
*
*The php suggested code:
<?php
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$my_url = "YOUR_POST_LOGIN_URL";
$video_title = "YOUR_VIDEO_TITLE";
$video_desc = "YOUR_VIDEO_DESCRIPTION";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$access_token = file_get_contents($token_url);
$post_url = "https://graph-video.facebook.com/me/videos?"
. "title=" . $video_title. "&description=" . $video_desc
. "&". $access_token;
echo '<form enctype="multipart/form-data" action=" '.$post_url.' "
method="POST">';
echo 'Please choose a file:';
echo '<input name="file" type="file">';
echo '<input type="submit" value="Upload" />';
echo '</form>';
?>*