PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] Post Image to the Facebook using Graph API

 February 04, 2022     facebook, facebook-graph-api, facebook-php-sdk, php     No comments   

Issue

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,


Solution

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



Answered By - Sahil Mittal
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing