直接发布在用户墙上 HTTP/1.0 400

发布于 2024-10-26 20:26:07 字数 2991 浏览 2 评论 0原文

我使用 facebook 指南制作 facebook 应用程序

当用户允许应用程序访问时,索引页面将其重定向到 tst.php,直接发布在用户墙上

我在 index.php 中使用以下代码:

index.php

<?php 

require_once 'facebook-php-sdk/src/facebook.php';

$facebook = new Facebook(array(    
     'appId' => 'app_id',  
     'secret' => 'app_secret',  
     'cookie' => true,
)); 

$app_id = app_id;

$canvas_page = 'http://apps.facebook.com/tstsample/tst.php';

$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" .
            $app_id . "&redirect_uri=" . urlencode($canvas_page) . 
            "&scope=email offline_access publish_stream";

$signed_request = $_REQUEST["signed_request"];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);   

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

if (empty($data["user_id"])) {
     echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
     echo ("Welcome User: " . $data["user_id"]);
} 

?>

以及下面是我在 tst.php 中使用的代码,用于直接在用户墙上发布:

tst.php

<?php

$feedurl = "https://graph.facebook.com/134875956586326";
$ogurl = "http://www.facebook.com/apps/application.php?id=134875956586326"; 
$app_id = “134875956586326”; 
$app_secret = "app_secret";

$mymessage = urlencode("Hello World!");

$access_token_url = "https://graph.facebook.com/oauth/access_token"; 
$parameters = "type=client_credentials&client_id=" .  
$app_id . "&client_secret=" . $app_secret;
$access_token = file_get_contents($access_token_url . 
                "?" . $parameters);

$apprequest_url = "https://graph.facebook.com/feed";
$parameters = "?" . $access_token . "&message=" . 
              $mymessage . "&id=" . $ogurl . "&method=post";
$myurl = $apprequest_url . $parameters;

$result = file_get_contents($myurl);
echo "post_id" . $result;

?>

在我运行应用程序后,出现两个错误,

  1. 我看到访问允许页面,而不是在索引页面上停止
  2. 按照我在上面的代码中提到的手动继续 tst.php 后,我收到 PHP 错误

以下是错误:

Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?type=client_credentials&client_id=�134875956586326�&client_secret=606636c88cd434c5140986d8472fc639) 
             [function.file-get-contents]: failed to open stream: HTTP request failed! 
                 HTTP/1.0 400 Bad Request in /home5/onkhuras/public_html/escribir/tstsample/tst.php
                     on line 14

Warning: file_get_contents(https://graph.facebook.com/feed?&message=Hello+World%21&id=http://www.facebook.com/apps/application.php?id=134875956586326&method=post) 
             [function.file-get-contents]: failed to open stream: HTTP request failed! 
                 HTTP/1.0 400 Bad Request in /home5/onkhuras/public_html/escribir/tstsample/tst.php
                     on line 21
post_id

如何解决这两个错误?

注意:我从 Facebook 获取这两个代码,并在 index.php 和 txt.php 中使用

I make facebook application using facebook guide

When user allow application access than the index page redirect it to tst.php which directly post on user wall

I use the below code in index.php:

index.php

<?php 

require_once 'facebook-php-sdk/src/facebook.php';

$facebook = new Facebook(array(    
     'appId' => 'app_id',  
     'secret' => 'app_secret',  
     'cookie' => true,
)); 

$app_id = app_id;

$canvas_page = 'http://apps.facebook.com/tstsample/tst.php';

$auth_url = "http://www.facebook.com/dialog/oauth?client_id=" .
            $app_id . "&redirect_uri=" . urlencode($canvas_page) . 
            "&scope=email offline_access publish_stream";

$signed_request = $_REQUEST["signed_request"];

list($encoded_sig, $payload) = explode('.', $signed_request, 2);   

$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

if (empty($data["user_id"])) {
     echo("<script> top.location.href='" . $auth_url . "'</script>");
} else {
     echo ("Welcome User: " . $data["user_id"]);
} 

?>

And below is the code which I use in tst.php which use to directly post on user wall:

tst.php

<?php

$feedurl = "https://graph.facebook.com/134875956586326";
$ogurl = "http://www.facebook.com/apps/application.php?id=134875956586326"; 
$app_id = “134875956586326”; 
$app_secret = "app_secret";

$mymessage = urlencode("Hello World!");

$access_token_url = "https://graph.facebook.com/oauth/access_token"; 
$parameters = "type=client_credentials&client_id=" .  
$app_id . "&client_secret=" . $app_secret;
$access_token = file_get_contents($access_token_url . 
                "?" . $parameters);

$apprequest_url = "https://graph.facebook.com/feed";
$parameters = "?" . $access_token . "&message=" . 
              $mymessage . "&id=" . $ogurl . "&method=post";
$myurl = $apprequest_url . $parameters;

$result = file_get_contents($myurl);
echo "post_id" . $result;

?>

And after i run the application two errors occur

  1. I see the access allow page than it stops on index page
  2. After manually going on the tst.php as I mentioned in the above code, I receive PHP errors

Below are the errors:

Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?type=client_credentials&client_id=�134875956586326�&client_secret=606636c88cd434c5140986d8472fc639) 
             [function.file-get-contents]: failed to open stream: HTTP request failed! 
                 HTTP/1.0 400 Bad Request in /home5/onkhuras/public_html/escribir/tstsample/tst.php
                     on line 14

Warning: file_get_contents(https://graph.facebook.com/feed?&message=Hello+World%21&id=http://www.facebook.com/apps/application.php?id=134875956586326&method=post) 
             [function.file-get-contents]: failed to open stream: HTTP request failed! 
                 HTTP/1.0 400 Bad Request in /home5/onkhuras/public_html/escribir/tstsample/tst.php
                     on line 21
post_id

How do I resolve these two errors?

Note: I get these two codes from Facebook which I use in index.php and txt.php

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

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

发布评论

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

评论(2

丿*梦醉红颜 2024-11-02 20:26:07

再次检查身份验证指南 - https://developers.facebook.com/docs/authentication/

您需要 /dialog/oauth 返回的代码才能从 https://graph.facebook.com/ 获取 access_token oauth/access_token

Check the authentication guide again - https://developers.facebook.com/docs/authentication/.

You need the code returned by /dialog/oauth to get an access_token from https://graph.facebook.com/oauth/access_token

软的没边 2024-11-02 20:26:07

我遇到了类似的问题(第二条错误消息),并通过使用 $ogurl 的应用程序 ID 解决了这个问题(tst.php 文件中的最后几行):

...
$apprequest_url = "https://graph.facebook.com/feed";
$parameters = "?" . $access_token . "&message=" . 
          $mymessage . "&id=" . $app_id . "&method=post";
$myurl = $apprequest_url . $parameters;
$result = file_get_contents($myurl);
echo "post_id" . $result;
?>

在我的情况下,这导致将消息发布到应用程序墙。

I had a similar problem (the second error message) and got it resolved by using my app id for $ogurl like this (the last few lines in your tst.php file):

...
$apprequest_url = "https://graph.facebook.com/feed";
$parameters = "?" . $access_token . "&message=" . 
          $mymessage . "&id=" . $app_id . "&method=post";
$myurl = $apprequest_url . $parameters;
$result = file_get_contents($myurl);
echo "post_id" . $result;
?>

In my case that lead to posting the message to the app wall.

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