将照片发布到FB结果需要上传文件错误 [英] Posting Photo to FB results in Requires upload file error

查看:251
本文介绍了将照片发布到FB结果需要上传文件错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,我认为是非常接近工作,但我不断收到以下错误:

I have the following code which I think is very close to working but I keep getting the following error:

{"error":{"message":"(#324) Requires upload file","type":"OAuthException"}}

但我不知道如何指定上传文件...我已经尝试了图像和源,都给出相同的错误。任何想法,帮助,建议将不胜感激,因为我一直在努力这几天,似乎不应该是这么难...

but I cannot figure out how to specify the upload file... I have tried both image and source, both give the same error. Any thoughts, help, suggestions would be greatly appreciated as I have been struggling with this for a couple of days and it seems like it should not be this hard...

<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>


推荐答案

您需要设置config var <$ c $在 fileUpload 至 true .com / docs / reference / php /rel =nofollow>文档这里此处。你还需要photo_upload权限; - )

You need to set the config var fileUpload to true when creating a new instance of the Facebook class as described in the documentation here and here. And you also need the photo_upload permission ;-)

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);

这篇关于将照片发布到FB结果需要上传文件错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆