【发布时间】:2011-01-31 11:24:21
【问题描述】:
我是 facebook api 的新手,我有一个小问题。 我正在尝试将图片上传到用户的相册。为此,我正在使用我在这里找到的 PHP 脚本,堆栈溢出:
$app_id = xxx;
$app_secret = "xxx";
$my_url = "http://apps.facebook.com/myapp/test.php";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url);
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);
//upload photo
$file= 'test.png';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);
print_r($args);
$ch = curl_init();
$url = 'https://graph.facebook.com/2038278/photos?access_token='.$access_token;
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);
//returns the photo id
print_r(json_decode($data,true));
但结果是:
Array ( [message] => 图片来自 应用程序 [test.png] => @/[mypath]/test.png ) 数组 ( [错误] => Array ( [type] => OAuthException [message] => 验证错误 应用。 ) )
这是什么意思?我需要为我的应用程序扩展权限吗?其中有哪些? 我给了它访问 basic 和 user_photos 的权限
非常感谢!
第二个脚本
$app_id = "xxx"; $app_secret = "xxx"; $my_url = "http://apps.facebook.com/myapp/test.php";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&scope=user_photos" ;
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);
$token = $access_token;
//upload photo
$file= 'test.png';
$args = array(
'message' => 'Photo from application',
);
$args[basename($file)] = '@' . realpath($file);
$ch = curl_init();
$url = 'https://graph.facebook.com/me/photos?access_token='.$token;
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);
print_r(json_decode($data,true));
【问题讨论】:
-
您好,您要上传用户现有相册中的照片吗?
-
嗨!测试是的,这就是我尝试过的,但我需要的是自动为我的应用创建一个相册
-
它确实说“验证应用程序错误” - 您确定问题不在于您的 Facebook 应用程序本身吗?也许它配置不正确,或者您没有将正确的密钥传递给 facebook 以进行初始身份验证步骤?