【问题标题】:Facebook API upload photo from URLFacebook API 从 URL 上传照片
【发布时间】:2012-07-03 14:46:49
【问题描述】:

我有一个带有照片库的网站,我想将每张照片(一张一张)上传到我的 Facebook 页面(不是墙)。我设法发布了一条消息,但现在我想通过从服务器上传现有图像 - 特定 URL(我不想在本地再次上传)将照片上传到 FB Page Wall。这可能吗?

【问题讨论】:

标签: facebook facebook-graph-api image-uploading


【解决方案1】:

是的,你可以做到
例子 在Graph Api Explorer
拨打电话,将网址设置为https://graph.facebook.com/me/photos
添加带有键 message 和值“任何自定义消息”的字段
添加另一个字段,键为 url,值为 https://appharbor.com/assets/images/stackoverflow-logo.png
点击提交

【讨论】:

  • 几乎 :) 照片上传到了我的个人墙上。如何上传到我的 Facebook 页面 (ilija.veselica.photography) 上的墙?谢谢
  • 这是正确的答案。由于 endPoint url 中的 /me/photos,照片会上传到您的个人墙上。如果您想要特定页面,请将 /me/ 替换为 /pageId/
  • Suberb.. 像魅力一样工作。谢谢!!
【解决方案2】:

您需要知道专辑 ID 并调用 POST 到:

https://graph.facebook.com/albumid/photos?access_token=$access_token

您会发现进入相册并查看 URL 的相册 ID。类似于https://www.facebook.com/media/set/?set=a.XXXXXXXXXXX.YYYY.ZZZZZZZZZZ&type=3

您的专辑 ID 是 XXXX。

【讨论】:

  • 这是专辑:facebook.com/media/set/… 所以,专辑 id 是 254503314567659。这是否意味着我打电话给 graph.facebook.com/254503314567659/photos?我进行了 access_token、消息和 url 参数,但仍然是同样的错误
  • 我觉得先在这里尝试比较容易:developers.facebook.com/tools/…
  • 我将 GET 更改为 POST 并添加了 2 个新参数:access_token 和 url。
  • 知道了!我一直在使用错误的 access_token(我将它用于我的个人帐户)。为了获得页面访问令牌,我调用了:graph.facebook.com/…,它列出了我所有页面的令牌。 XY = 我的个人账户 access_token
【解决方案3】:

这是我用的:

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true,
    'fileUpload' => true,
));

$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.

$facebook->setFileUploadSupport(true);

if($user == 0) {
    // If the user is not connected to your application, redirect the user to authentication page
    /**
     * Get a Login URL for use with redirects. By default, full page redirect is
     * assumed. If you are using the generated URL with a window.open() call in
     * JavaScript, you can pass in display=popup as part of the $params.
     * 
     * The parameters:
     * - redirect_uri: the url to go to after a successful login
     * - scope: comma separated list of requested extended perms
     */

    $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));

    echo ("<script> top.location.href='".$login_url."'</script>");

} else {
    // if the user is already connected, then fetch access_token and user's information or show some content to logged in user.
    try
    {
        $access_token = $facebook->getAccessToken(); // Gives you current user's access_token

        $user = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application.

    } catch(FacebookApiException $e){
        $results = $e->getResult();
        // Print results if you want to debug.
    }

}

$img = './upload/'.$image_path;
    $args = array(
       'message' => 'Some Message',
        'access_token'=>urlencode($access_token),
    );
    $args[basename($img)] = '@'.realpath($img);

    $ch = curl_init();
    $url = 'https://graph.facebook.com/me/photos';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
    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);
    $response = json_decode($data,true);

【讨论】:

  • 由于您在这里发布服务器端代码,我假设“他们”实际上是“我”。在这种情况下,以这种方式实际链接到您的网站是完全不合适的,因为它与回答这个问题没有远程关系。但我不会将此标记为垃圾邮件,而是将其编辑为适当的答案。
【解决方案4】:
            $config = array('appId' => $config['App_ID'],'secret' => $config['App_Secret']);

            $facebook = new Facebook($config);

            // sets our access token as the access token when we call 
            // something using the SDK, which we are going to do now.
            $facebook->setAccessToken($access_token); 

            $page_id = "XXXXXXXXXXXXXXX";

            $page_access_token = "";

            $result = $facebook->api("/me/accounts");

            foreach($result["data"] as $page) {
                if($page["id"] == $page_id) {
                    $page_access_token = $page["access_token"];
                    break;
                }
            }

            $facebook->setFileUploadSupport(true);
            $photo = "http://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png";
            $args = array(
                'access_token'  => $page_access_token,
                'message'       => "message here",
                'url' => $photo,
            );
            $post = $facebook->api("/$page_id/photos","post",$args);

【讨论】:

    猜你喜欢
    • 2012-09-28
    • 2012-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多