i am trying to post image to a facebook fun page, it's working will on my own wall but the fun page not and have no problem with posting text to fb fun page wall.
$photo_url = "http://www.google.com/logo.png";
$photo_caption = $text;
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if (!$code)
{
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else
{
$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 = file_get_contents($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/{FUNPAGE-ID}/photos?"
. "url=" . urlencode($photo_url)
. "&message=" . urlencode($photo_caption)
. "&method=POST"
. "&access_token=" .$access_token;
echo '<html><body>';
echo file_get_contents($graph_url);
echo '</body></html>';
}
what can i do i think it's about the graph_url
You need to provide the data as encoded binary of the image itself.
The URL parameter only adds a link to the POST as a part of the text or annotation, it will not retrieve and add the photo istelf from that URL!
add something like
...
"url=" . urlencode(file_get_contents($photo_url));
...
And: furthermore the Method must be POST, I'm not quite sure whether "file_get_contents" is going to POST the data and not just doing a GET as the name imposes.
Related
I have an app that posts to people's Facebook pages and profiles. The below code has been in place for about 2 years. Now, for new users, when my app logs into Facebook and asks for permissions, it does not ask for manage_pages, even though its explicitly listed below.
Did something on the Facebook side change which requires me to change my code?
Thanks,
Brian
if (isset($_REQUEST["code"]))
$code = $_REQUEST["code"];
if(empty($code)) {
$_SESSION['state'] = md5(uniqid(rand(), TRUE)); // CSRF protection
$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="
. $FBAPPID . "&redirect_uri=" . urlencode($my_url) . "&state="
. $_SESSION['state'] . "&scope=publish_actions,manage_pages,publish_pages";
header("Location: " . $dialog_url);
exit;
}
if($_SESSION['state'] && ($_SESSION['state'] === $_REQUEST['state'])) {
// state variable matches
$token_url = "https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $FBAPPID . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $FBSECRET . "&code=" . $code;
$response = file_get_contents($token_url);
... (more stuff)
I got the code from the Facebook page to publish the Photo to the user's wall (account).
Below is my PostToFB.php file:
<?php
include_once "facebook.php";
ini_set("display_errors",0);
//configuring application to post.
$app_id = "YOUR_APP_ID";
$app_secret = "YOUR_APP_SECRET";
$post_login_url = "YOUR_REDIRECT_URL";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/me/photos?"
. "access_token=" .$access_token;
echo '<html><body>';
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
echo '</body></html>';
}
?>
But this is not working. I get below Output:
{
"error": {
"message": "(#200) Permissions error",
"type": "OAuthException",
"code": 200
}
}
Can you please help me to solve this?
Thank You,
This code works absolutely fine. Only probably reason seems to me is that you have not granted the permission for the photo upload. Check which permissions you have granted to your app from here: https://www.facebook.com/settings?tab=applications
If still didnt helped, you can create a new app and try again; since this code is correct.
Edit:
publish_stream is deprecated, try using publish_actions
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 am creating an app where the user can upload an image, that will be stored in apps profile page, and in the user profile. I'm using below code to do this work
<?php
$app_id = "XXXXXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXXXXXX";
$post_login_url ="XXXXXXXXXXXXXXXXXXXXXXX";
$code = $_REQUEST["code"];
//Obtain the access_token with publish_stream permission
if(empty($code)){
$dialog_url= "http://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&scope=publish_stream";
echo("<script>top.location.href='" . $dialog_url
. "'</script>");
}
else {
$token_url="https://graph.facebook.com/oauth/access_token?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode( $post_login_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
// first get your album id, let's assume you need to create it
// create this before hand and you can just reference the id
$attachment = array('access_token'=> ACCESS_TOKEN, 'name'=>$ablum_name);
try{
$album_resp = $facebook->api("/{$this->page_id}/albums", 'POST', $attachment);
}catch(Exception $e){
throw new Exception("Failed to create album: ". $e->getMessage());
}
$album_id = $album_resp['id'];
// Show photo upload form to user and post to the Graph URL
$graph_url= "https://graph.facebook.com/photos?"
. "access_token=" .$access_token;
echo '<form enctype="multipart/form-data" action="'
.$graph_url .' "method="POST">';
echo 'Please choose a photo: ';
echo '<input name="source" type="file"><br/><br/>';
echo 'Say something about this photo: ';
echo '<input name="message"
type="text" value=""><br/><br/>';
echo '<input type="submit" value="Upload"/><br/>';
echo '</form>';
}
?>
With this code it is going to the user's profile but not to the app's profile page. Can any one please tell what is wrong with it? Or is there another way to do it?
im trying to import "log in with facebook" opportunity to my website, im using http://developers.facebook.com/docs/authentication, but still can not make it work.
I register my website and have app id and app secret.
I have the following code in my login form:
<img src="images/fb-login-button.png" />
facebook.php file:
<?php
$app_id = 1000000000000;
$app_secret = "asdasdasdasd";
$my_url = "http://xxxx.xx/";
$code = $_REQUEST["code"];
if(empty($code)) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href='" . $dialog_url . "'</script>");
}
$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret="
. $app_secret . "&code=" . $code;
$access_token = file_get_contents($token_url);
$graph_url = "https://graph.facebook.com/me?" . $access_token;
$user = json_decode(file_get_contents($graph_url));
echo("Hello " . $user->name);
?>
It returns message "undefined index code" and I have no idea where and what to change.
Please, help!
The error you're getting is telling you that the "code" parameter you're looking for in the request:
$code = $_REQUEST["code"];
is not being submitted.. E.g. you either need to post "code" to the page or pass it via GET using facebook.php?code=something
To avoid running into errors when the parameter "code" is not sent, your code could look like:
if(!isset( $_REQUEST["code"] ) ) {
$dialog_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href='" . $dialog_url . "'</script>");
} else {
$code = $_REQUEST["code"];
}
Hope that helps..