使用 Facebook Graph API 上传照片时出现异常

发布于 2024-09-08 15:26:42 字数 1764 浏览 3 评论 0原文

我想在应用程序的默认相册中将用户的照片上传到 Facebook。此处的发布对此进行了描述: http://developers.facebook.com/docs/reference/api/photo

该方法已在这里得到解答:如何我使用 Facebook Graph API 将照片上传到相册。我正在使用以下内容:

$args = array(
  'message' => 'Photo Caption', 
  'image' => '@'.realpath("image.png")
);
$data = $facebook->api('/me/photos', 'post', $args);

但是,当我尝试执行此操作时,我收到异常“(#324) 需要上传文件”。我有一个有效的会话,并且有publish_stream 和 user_photos 权限。我可以使用 API 检索数据。该图像文件绝对有效,因为它可以使用 file_get_contents(realpath("image.png")) 加载。

我已经尝试过这个解决方案,使用curl,它工作得很好: 使用 Facebook 的 Graph API 将照片上传到相册

$args = array(
  'message' => 'Photo from application',
  'pic.png' => '@'.realpath('pic.png')
);
$tok = $session['access_token']
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$tok;
$ch = curl_init();
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);

与 Facebook 的 PHP SDK curl 相比,它看起来像这样(使用相同的 $args 和 $url):

$ch = curl_init();
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = http_build_query($args, null, '&');
$opts[CURLOPT_URL] = $url;
curl_setopt_array($ch, $opts);
$data= curl_exec($ch);

为什么 PHP 版本不起作用?看起来 http_build_query() 函数正在干扰加载图像。我对curl了解不够,无法理解这里发生的事情。

I would like to upload a photo to facebook for a user in the default album for an application. This is described under publishing here:
http://developers.facebook.com/docs/reference/api/photo

The method has been answered here: How can I upload photos to album using Facebook Graph API. I am using the following:

$args = array(
  'message' => 'Photo Caption', 
  'image' => '@'.realpath("image.png")
);
$data = $facebook->api('/me/photos', 'post', $args);

However I get the exception "(#324) Requires upload file" when I attempt this. I have a valid session and I have the publish_stream and user_photos permissions. I can retrieve data using the API. The image file is definitely valid because it can be loaded with file_get_contents(realpath("image.png")).

I've tried this solution, using curl, which works perfectly:
Upload Photo To Album with Facebook's Graph API

$args = array(
  'message' => 'Photo from application',
  'pic.png' => '@'.realpath('pic.png')
);
$tok = $session['access_token']
$url = 'http://graph.facebook.com/'.$album_id.'/photos?access_token='.$tok;
$ch = curl_init();
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);

Compared with Facebook's PHP SDK curl which looks like this (using the same $args and $url):

$ch = curl_init();
$opts = self::$CURL_OPTS;
$opts[CURLOPT_POSTFIELDS] = http_build_query($args, null, '&');
$opts[CURLOPT_URL] = $url;
curl_setopt_array($ch, $opts);
$data= curl_exec($ch);

Why doesn't the PHP version work? Looks like the http_build_query() function is interfering with loading the image. I don't know enough about curl to understand what's going on here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

゛清羽墨安 2024-09-15 15:26:42

我很高兴我遇到了同样的问题
您必须将 fileUpload 参数设置为 true !

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

I'm so glad I went trough the same problem
You have to set the fileUpload param to true !

$facebook = new Facebook(array(
            'appId'  => $facebookapi_id,
            'secret' => $facebookapi_secret,
            'fileUpload' => true,
            'cookie' => true
          ));  
忆依然 2024-09-15 15:26:42

Facebook 有意使用 http_build_query() 将 POST 字段转换为 GET 字符串,以阻止以 @ 开头的字段被意外或恶意地用于上传文件。 这是 GitHub 问题。

对此问题的快速解决方法是删除SDK 中 src/facebook.php 中的 http_build_query()

$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');

变为:

$opts[CURLOPT_POSTFIELDS] = $params;

但是,如果您这样做,您应该采取措施过滤以 @ 开头的用户生成的消息。例如,您可以在每条消息的前面添加一个空格。

Facebook have intentionally transformed the POST fields to a GET string using http_build_query() to stop fields beginning with @ being used to accidentally or maliciously to upload files. Here's the GitHub issue.

A quick fix for this is to remove the http_build_query() from src/facebook.php in the SDK:

$opts[CURLOPT_POSTFIELDS] = http_build_query($params, null, '&');

Becomes:

$opts[CURLOPT_POSTFIELDS] = $params;

However if you do this you should take action to filter user generated messages that start with @. For example you could add a space to the front of each message.

把时间冻结 2024-09-15 15:26:42

如果您使用 graph api 上传照片,则会收到错误 (#200) 用户必须已接受 TOS。

但是,如果您使用旧的 REST API,只需将 url 更改为 https://api .facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX 如果您使用上面的示例。

If you use graph api to upload the photo it will getting the error (#200) User must have accepted TOS.

However if you use old rest api, just changing the url to https://api.facebook.com/method/photos.upload?access_token=xXXXXXXXXXXX if you use above example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文