Post directly on user wall HTTP/1.0 400 - 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
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=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

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.

Related

how to handle expired access token in facebook

I found following code from here, it is supposed to work and it has 2 errors.
First one is : Trying to get property of non-object on line 38, which starts from
if ($decoded_response->error)
Second error is Trying to get property of non-object on line 54 which starts from
echo("success" . $decoded_response->name);
So following code is my full code that gets access token.
<html>
<head></head>
<body>
<?php
require_once('src/facebook.php');
include_once('src/base_facebook.php');
$app_id = "413030352142786";
$app_secret = "518852750047ba579316a19d81c1e115";
$my_url = "134061970136571";
// known valid access token stored in a database
$access_token = "CAAF3ph9rwcIBAD5GrDLZBI6TFKlSb4jccEmIorCVjtfIeKIq7FjGKntMI5qnZCaNlXL8qy2g0gbjof3sRJYmgoDckG5ZBWpZBHjgZAn1e3sHTyxDdWwcCaZAiCylP5ayoWIMZBZCgNBL71I3myFYoDUYNZB4dH0xKJjAZD";
#$code = $_REQUEST["code"];
// If we get a code, it means that we have re-authed the user
//and can get a valid access_token.
if (isset($code)) {
$token_url="https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code . "&display=popup";
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
}
// Attempt to query the graph:
$graph_url = "https://graph.facebook.com/me?"
. "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
//Check for errors
if ($decoded_response->error) {
// check to see if this is an oAuth error:
if ($decoded_response->error->type== "OAuthException") {
// Retrieving a valid access token.
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href='" . $dialog_url
. "'</script>");
}
else {
echo "other error has happened";
}
}
else {
// success
echo("success" . $decoded_response->name);
echo($access_token);
}
// note this wrapper function exists in order to circumvent PHP’s
//strict obeying of HTTP error codes. In this case, Facebook
//returns error code 400 which PHP obeys and wipes out
//the response.
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
?>
</body>
</html>
Thank you for your reply !!!

App_access_token Always Returns False

According to the Facebook docs, using App_access_token is as simple as:
$appid = 'xxx';
$secret = 'yyyyyyyyy';
$facebook = new Facebook(array(
'appId' => $appid,
'secret' => $secret,
'cookie' => true,
));
$session = $facebook->getUser()>0;
if($session){
$access_token = 'access_token='.$facebook->getAccessToken();
$app_access_token = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=$appid&client_secret=$secret&grant_type=client_credentials");
}
My problem is that $access_token is fine but $app_access_token always returns false, what am I doing wrong?
-EDIT-
If I open the app_access_token URL in my browser I can see the access token! It seems to fail the file_get_contents... ?
The file_get_contents could be creating problems, may be due to some php config settings. (Discussion here: file_get_contents returns empty string)
I'll recommend you to use cURL instead, something like that-
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
echo get_content('http://example.com');
If the problem persists, re-check the values of app_id and secret in the url.
I file_get_contents not working try this :
Enable php ini setting for allow_url_fopen, then restart the server.
Here is the working code :
<?php
$facebook_appid = "facebook_appid"; // Facebook appplication id
$facebook_secret = "facebook_secret"; // Facebook secret id
$redirect_uri = "https://192.168.75.44/facebook_login/fb_login.php"; // return url to our application after facebook login ## should be SAME as in facebook application
//$cancel_url = ""; // redirect url if user denies the facebook login ## should be SAME as in facebook application
$scope = "user_photos,email,user_birthday,user_online_presence,offline_access"; // User permission for facebook
$code = $_REQUEST["code"]?$_REQUEST["code"]:"";
if(empty($code)) {
$_SESSION['state'] = time(); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id=". $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri) . "&state=". $_SESSION['state'] . "&scope=".$scope;
header("location:".$dialog_url);
}
if($_SESSION['state'] && ($_SESSION['state'] == $_REQUEST['state'])) {
$token_url = "https://graph.facebook.com/oauth/access_token?". "client_id=" . $facebook_appid . "&redirect_uri=" . urlencode($redirect_uri). "&client_secret=" . $facebook_secret . "&code=" . $code;
$response = #file_get_contents($token_url);
$params = null;
parse_str($response, $params);
echo $params['access_token'];
}
?>

Facebook Access Tokens

I am trying to get the Facebook access tokens useing the bellow code:
I did create my own function before but it came back with the same error and i can't work out what is the problem.
function getAccessToken(){
$app_id = FB_APP_ID;
$app_secret = FB_SECRET_ID;
$my_url = FB_PAGE_URL;
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
if($_REQUEST['state'] == $_SESSION['state']) {
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret . "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me?access_token="
. $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
}
Taken from Server-Side Authentication but i keep getting this error:
Warning: file_get_contents(https://graph.facebook.com/oauth/access_token?
client_id=ID HERE&redirect_uri=URLHERE&cli
ent_secret=SECRECT&code=AQCI5rNgw9zCPHWGozeT59asg7_022u5tVc5XSef49BiX
IaF5_MAMqFwsqOAquUHgjOu_99ONwUV6IC7k-jV6DsWf9ni3jm8t59aHCBp1jrFaDthPbIKLNLQ-
fZgB5MLh1le5BAPKj_l57jhTLTBfOdxRU30mFCMYzMch8MYFpCmJ9GrjSSGwt0OKb_LNqMoRf8) [function.file-
get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
Anyone know the fix?
It might be a problem with the file_get_contents method, since the url you are constructing looks ok.
If you search for "facebook file_get_contents failed to open stream" you'll get quite a few results on the matter, a lot right here on stack overflow.
It looks like a HTTPS problem with the file_get_contents as states in this thread: [function.file-get-contents]: failed to open stream.
Maybe try some other method of issuing http requests? Maybe curl?
Just to test things, try to use curl from the command line with the same url you tried with the php script and see what you get.
Looking at the request response, you haven't defined your Facebook application settings.
You're using FB_APP_ID, FB_SECRET_ID and FB_PAGE_URL when you request a token, but in the response I can see these are defined as: "ID HERE", "SECRECT" and "URLHERE" respectively.
I have managed to fix this issue using the PHP SDK,
and after a long battle with the SDK to get it to do what i wanted.
I did also open another question with the same idea but with another problem.
This problem has now been solved.
Check out this question for the answer to this problem: Facebook Access Token Server Side Authentication

Canvas app to echo user id back to them. Been trying for hours

I got given a code from a friend that displayed the app users birth date on the app canvas.
I have tried to change it in order to echo the users own ID. It works for myself (the creator) and it also works for a friend that i have designated as a developer. But for anyone else it brings up this error:
Warning: file_get_contents(https://graph.facebook.com/me?
fields=id&access_token=228073413942115|460d2e1ffd0b4cc6b5cb480d53f60d6e)
[function.file-get-contents]: failed to open stream: HTTP request failed!
HTTP/1.0 400 Bad Request in /home/robsdmr/public_html/fb/index.php on line 34
The code is as follows.
<?php
require_once("src/facebook.php");
$app_id = "xxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";
$canvas_page = "https://apps.facebook.com/roblewtest/" .$app_id;
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
$access_token = $facebook->getAccessToken();
$auth_url = "https://www.facebook.com/dialog/oauth?scope=user_id&client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page);
$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 {
}
$graph_url = "https://graph.facebook.com/me?fields=id&access_token=" . $access_token;
$result = json_decode(file_get_contents($graph_url));
echo "{$result->id}"
?>
I am pretty new to this so pointers would be great
I have a feeling that it is the scopes that i am not quite to grips with, but i really need this working.
Thanks a lot guys n gals.
R.
Try using SDK's graph methtod:
$result = $facebook->api('/me?fields=id');
And also to get login url you could use:
$auth_url = $facebook->getLoginUrl();
Documentation:api(), getLoginUrl()
Also simplified you code, see here.

Suddenly started getting 400 Bad Request Error

A few days ago I suddenly started getting 400 errors in my app. It was completely fine before.
Warning: file_get_contents(https://graph.facebook.com/me/accounts?access_token=XXXXXXXXXXXX) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
It's really frustrating. I can get the access_token just fine but I can't get "https://graph.facebook.com/me/accounts?access_token=XXXXXXXXXXXX". I know that URL is correct because I can see the content by loading it on the browser.
if(!$code){
$display = 'page';
$scope = 'manage_pages';
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$oauth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'];
echo "<script type=\"text/javascript\">\ntop.location.href = \"$oauth_url\";\n</script>";
};
if ($code && $_REQUEST['state'] == $_SESSION['state']){
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret . "&code=" . $code . "&redirect_uri=" . $my_url;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$graph_url = "https://graph.facebook.com/me/accounts?access_token=" . $params['access_token'];
$user = json_decode(file_get_contents($graph_url));
$pages_data = $user->data;
}
Any help will be VERY appreciated!

Categories