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!
Related
The error I'm getting is:
Curl error: couldn't connect to hostCurl error: couldn't connect to
host
I have tried almost everything to solve this problem but it doesn't seem to work file_get_contents doesn't work either but its enabled.
$response = curl_exec($ch);
if($response === false) {
echo 'Curl error: ' . curl_error($ch);
} else {
$response = $response;
}
curl_close($ch);
return $response;
}
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$fanpage_id ='325145047546976';
$post_login_url = "http://www.feriajanos.com/src/teszt.php";
$photo_url = "http://www.feriajanos.com/kepek/mas/posztok/poszt20130412104817.jpg";
$photo_caption = "Feri A János";
$code = $_REQUEST["code"];
if (!$code) {
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream,manage_pages";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
if(isset($_REQUEST['code'] )) {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&client_secret=" . $app_secret
. "&redirect_uri=" . urlencode( $post_login_url)
. "&code=" . $code;
$response = curl($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// POST to Graph API endpoint to upload photos
$graph_url= "https://graph.facebook.com/me/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo curl($graph_url);
echo '</body></html>';
}
?>
I am trying to get Facebook profile picture . I am using the following with my value in place of ######### . After running the PHP i get a message "Hello".
I want that i get my name also with it like "Hellow Aditya" along with my profile picture. what should i edit to get that ? Also would it be possible to save the profile picture on the sever itself?
<?php
$app_id = ###############;
$app_secret = "#################";
$my_url = "###################";
session_start();
$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.");
}
?>
You can get it this way:
http://graph.facebook.com/[USER ID]/picture
I use this code below.
$app_id = my_app_id;
$app_secret = my_app_secret;
$canvas_page = my_canvas_page;
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) . "&scope=email,read_stream,publish_stream";
echo("<script> top.location.href='" . $auth_url . "'</script>");
$code = $_REQUEST["code"];
$file = "help.txt";
$fh = fopen($file, "w+") or die("File ($file) does not exist!");
fwrite($fh, $code);
fclose($fh);
$_REQUEST["code"] return empty and i have error 107 ( net::ERR_SSL_PROTOCOL_ERROR). But real url contain code value
(https://plazma-tambov.ru/advanced/fb/?code=AQB_w2iFHofA8rzlr9FGD0EmA_BtWVhMmAJgsWo-YTgakgJ-YXibkd1_634pYZZL8UDds9ZlRHtKiWunGz5-2oWfKsrxpaur75t2PFPY9INuqCAL5kBtJsS_Bt9Vf-2WvHkDBtLYgzhUSk3InuV8IU3V6dwu9upGvH6ETDMLBY124JJBM3VMO6EF0eCjZg4ZsNU#=)
If i add next code
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id . "&redirect_uri=" . urlencode($canvas_page)
. "&client_secret=" . $app_secret . "&code=" . $code;
echo("<script> top.location.href='" . $token_url . "'</script>");
the real url contain empty for code variable
(https://graph.facebook.com/oauth/access_token?client_id=MY_APP_ID&redirect_uri=https%3A%2F%2Fplazma-tambov.ru%2Fadvanced%2Ffb%2F&client_secret=MY_APP_SECRET&code=)
Why S_REQUEST["code"] returns nothing ?
In my case, it often helps to add
$opts[CURLOPT_SSL_VERIFYPEER] = false;
$opts[CURLOPT_SSL_VERIFYHOST] = 0;
in the "makeRequest" method in base_facebook.php. Also you should make sure that $_SERVER is NOT damaged by some script. It won't work if the data in $_SERVER is escaped in a wrong way.
See also this answer.
I want that users can authenticate through their fb account, and i tried their demo script:
$app_id = "*****";
$app_secret = "*****";
$my_url = "http://localhost/fb/fb.php";
session_start();
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection
$dialog_url = "http://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);
echo "$user->name";
}
else {
echo("The state does not match. You may be a victim of CSRF.");
}
The problem is that after a successful login only the "Hello" string is displayed, but my $user->name is not showed.
Even though this is official FB code (I didn't realize that at the start, sorry), I recommend you debug it to see what goes wrong.
Activate error reporting
Remove the # in front of all calls - it suppresses the output of errors
Look what address $graph_url contains and whether you can access it in your browser
See whether the call to $graph_url works out (output the result)
See whether the json_decode() works out (output the result)
if this is a global or Facebook issue, you may need to talk to Facebook about what is wrong with their demo code.
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.