我使用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;
?>
在我运行应用程序后,发生了两个错误
以下是错误:
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中使用
答案 0 :(得分:0)
再次检查身份验证指南 - https://developers.facebook.com/docs/authentication/。
您需要/ dialog / oauth返回的代码才能从https://graph.facebook.com/oauth/access_token获取access_token
答案 1 :(得分:0)
我遇到了类似的问题(第二条错误消息)并通过使用我的应用程序ID为$ ogurl解决了这个问题(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;
?>
在我的情况下,导致将消息发布到应用程序墙。