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
- I see the access allow page than it stops on index page
- 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=1348759565开发者_如何学运维86326&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
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
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.
精彩评论