Ashish Shah
Ashish Shah

Reputation: 152

Post Image to the Facebook using Graph API

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,

Upvotes: 1

Views: 4448

Answers (1)

Sahil Mittal
Sahil Mittal

Reputation: 20753

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

enter image description here

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

Upvotes: 1

Related Questions