【问题标题】:facebook upload photo脸书上传照片
【发布时间】: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 以进行初始身份验证步骤?

标签: php facebook upload


【解决方案1】:

我认为您需要您的应用程序请求名为publish_stream 的权限。还有另一个名为photo_upload 的权限,但据我所知,这被认为是旧的(但可能仍然有效)

【讨论】:

  • 谢谢!我已经尝试了两种权限,都没有,同样的例外
  • 也许你想看看其他一些关于如何使用 Facebook Graph API 上传图片的教程:daipratt.co.uk/facebook-api-upload-photo
【解决方案2】:

亲爱的,当您将任何照片从您的应用程序上传到 Facebook 时,Facebook 会自动使用您的应用程序名称创建一个相册。你不需要做任何事情。只需从您的应用程序上传任何照片,然后转到您的个人资料,查看所有相册,然后您将看到 facebook 已使用您的应用程序名称创建了一个相册,并且您上传的照片位于其中。

【讨论】:

  • 我知道这一点,为此我已经构建了另一个可以自动创建专辑的脚本。但情况相同——它不起作用。我将它添加到问题中,也许我做错了什么
【解决方案3】:

如果我是你,我不会那样做。至少对我来说,cURL 方法已被证明有点太复杂了。这些是必须采取的步骤,您可以在下面看到一篇关于这个确切问题的最近博客文章的链接:

  1. 获取用户的user_photospublish_stream(不是必需的,但对未来的发展有好处)。
  2. 代表用户创建相册,其中将包含您要上传的照片。
  3. 获取您刚刚创建的相册的相册 ID。
  4. 指定照片的参数:随附的消息、文件在服务器上的位置,以及您可以在 this docs reference about publishing photos (bottom of page) 中查找的其他一些内容。
  5. 使用给定参数上传照片。

Dai Pratt's excellent tutorial on photo uploading via the New Graph API

重要提示:上面的脚本有一个严重的缺陷——它不会检查相册是否已经存在,以防你想代表用户上传多张照片。将用户 ID 和创建的相册 ID 存储在数据库中很重要,因此您可以在每次用户希望上传图像时查询它,并相应地创建或不创建相册。

编辑:现在,在我发布之后,我在此处的评论中看到了确切的链接。无论如何-希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    • 2015-12-11
    • 2016-03-10
    • 1970-01-01
    相关资源
    最近更新 更多