我有以下代码,我认为非常接近工作,但我不断收到以下错误:
{"error":{"message":"(#324) Requires upload file","type":"OAuthException"}}
但我无法弄清楚如何指定上传文件…我尝试了图像和源,都给出了同样的错误.任何想法,帮助,建议都会非常感激,因为我已经在这几天苦苦挣扎,似乎不应该这么难……
<html>
<head>
<title>Photo Upload</title>
</head>
<body>
<?php
$app_id = "MY APP ID GOES HERE";
$app_secret = "MY APP SECRET GOES HERE";
$post_login_url = "MY URL GOES HERE";
$album_name = "Test Album";
$album_description = "Blah Blah event Photos";
$photo_source = realpath("C:\\Users\\Public\\Pictures\\Sample Pictures\\Lighthouse.jpg");
$photo_message = 'Here is the lighthouse';
$code = $_REQUEST["code"];
echo "code ==>" . $code . '<br/><br/>';
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($post_login_url)
. "&scope=publish_stream,user_photos";
echo("<script>top.location.href='" . $dialog_url .
"'</script>");
}
else {
$token_url= "https://graph.facebook.com/oauth/"
. "access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
echo "access_token ==>" . $access_token . '<br/><br/>';
// Create a new album
$graph_url = "https://graph.facebook.com/me/albums?"
. "access_token=". $access_token;
$postdata = http_build_query(
array(
'name' => $album_name,
'message' => $album_description
)
);
$opts = array('http' =>
array(
'method'=> 'POST',
'header'=> 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = json_decode(file_get_contents($graph_url, false, $context));
// Get the new album ID
$album_id = $result->id; //upload photo
echo "album_id ==>" . $album_id . '<br/><br/>';
// Upload the photo
$args = array( 'message' => $photo_message,
'image' => $photo_source
);
$ch = curl_init();
$url = 'https://graph.facebook.com/'.$album_id.'/photos?
access_token='.$access_token;
echo "url ==>" . $url . '<br/><br/>';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);
echo "data ==>" . $data . "<br>";
}
?>
</body>
</html>
解决方法:
在创建Facebook类的新实例时,您需要将config var fileUpload设置为true,如documentation here和here中所述.您还需要photo_upload权限;-)
require_once("facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = true; // <-- set this to 'true' otherwise it won't work
$facebook = new Facebook($config);