Publishing To User's Wall Without Being Online/Logged-in - Facebook Sharing Using Graph API

20,671

I suggest you start learning how Facebook Graph API works first.

  1. Facebook will NEVER share the user password with you!
  2. If you just need to give the user the possibility to share link, then just use the like plugin. You may also find more interesting social plugins to use in your website.
  3. If you use the like plugin, it won't open any popups and it would post the link directly to the user's wall.
  4. You could always use the Feed Dialog
  5. Start reading the Facebook Documentation

Now to post on the user's wall (on his behalf) without him being logged-in, you need the following:

  1. app access_token
  2. publish_stream permission, NO NEED for the long-lived access token:

Enables your app to post content, comments, and likes to a user's stream and to the streams of the user's friends. This is a superset publishing permission which also includes publish_actions. However, please note that Facebook recommends a user-initiated sharing model. Please read the Platform Policies to ensure you understand how to properly use this permission. Note, you do not need to request the publish_stream permission in order to use the Feed Dialog, the Requests Dialog or the Send Dialog.

Require the permission:
This can be done in multiple of ways:
Using the Login Plugin:

<div class="fb-login-button" data-show-faces="true" data-width="200" data-scope="publish_stream" data-max-rows="1"></div>

Server-side login (Redirect to the OAuth Dialog):

https://www.facebook.com/dialog/oauth?
     client_id=YOUR_APP_ID
     &redirect_uri=YOUR_URL
     &scope=publish_stream
     &state=SOME_ARBITRARY_BUT_UNIQUE_STRING

PHP-SDK:

$loginUrl = $facebook->getLoginUrl(array("scope"=>"publish_stream"));

JS-SDK through the FB.login method:

 FB.login(function(response) {
   if (response.authResponse) {
     console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       console.log('Good to see you, ' + response.name + '.');
       FB.logout(function(response) {
         console.log('Logged out.');
       });
     });
   } else {
     console.log('User cancelled login or did not fully authorize.');
   }
 }, {scope: 'publish_stream'});

Publishing:

$USER_ID = "XXXXXXXX"; // Connected once to your APP and not necessary logged-in at the moment
$args = array(
    'message'   => 'Hello from app',
    'link'      => 'http://www.masteringapi.com/',
    'caption'   => 'Visit MasteringAPI.com For Facebook API Tutorials!'
);
$post_id = $facebook->api("/$USER_ID/feed", "post", $args);

Note:
While it's possible to post without the user's presence always remember Facebook recommends a user-initiated sharing model

Share:
20,671

Related videos on Youtube

Harish
Author by

Harish

I'm basically a PHP Programmer, with certain interest in Mysql and a huge passion for Jquery

Updated on November 12, 2020

Comments

  • Harish
    Harish over 3 years

    Hi i'm new to facebook sharing. I want to make a user log in using his uid and then store these for further uses

    The primary purpose is to share links in the user's wall

    next time onwards without showing the facebook window i want to post in to his wall through ajax.

    How is it possible any help regarding this will be appreciated!

    EDIT

    I used the example given and then tried curl function

    got this error

    HTTP/1.1 403 Forbidden
    Cache-Control: no-store
    Content-Type: text/javascript; charset=UTF-8
    Expires: Sat, 01 Jan 2000 00:00:00 GMT
    P3P: CP="Facebook does not have a P3P policy. Learn why here: http://fb.me/p3p"
    Pragma: no-cache
    WWW-Authenticate: OAuth "Facebook Platform" "insufficient_scope" "(#200) 
    This API call requires a valid app_id."
    Set-Cookie: datr=g4JBTb4OsRZxBAztV7iIkpWg; expires=Sat, 26-Jan-2013 14:34:43 GMT;
    path=/; domain=.facebook.com; httponly
    X-Cnection: close
    Date: Thu, 27 Jan 2011 14:34:43 GMT
    Content-Length: 93
    
    {"error":{"type":"OAuthException","message":"(#200) 
    This API call requires a valid app_id."}}
    

    EDIT SO THAT SOMEONE MAY FIND USEFUL

    FACEBOOK CONNECTION

    $facebook_connect =  array(
       'appId'  => 'YOUR APP_ID',
       'secret' => 'YOUR SEC KEY',
              'access_token'=>"USER'S ACCESS TOKEN",
       'cookie' => true
     );
    

    POST TO USER WALL

    $facebook->api("/PROFILE_ID/feed", "post", array(
    "message"=>"Hi Friends Join ****",
    "name"=>"You're invited to join ****!",
    "link"=>"www.****.com",
    "description"=>"Great site",
    "picture"=>"http://www.****.com/logo.jpg",
    "caption"=>"Join *****"
    )
    
  • Harish
    Harish about 13 years
    sorry i typed like that what i want is offline access to a user's acount so that i can post to his account evan if he is not logged in!edited the question
  • ifaour
    ifaour about 13 years
    @Harish: what server side language are you using? PHP?
  • Harish
    Harish about 13 years
    hi i used the example and tried curl to post to user wall got me one error can you please tell me what is this?
  • Harish
    Harish about 13 years
    but where i have to specify the access token?
  • Harish
    Harish about 13 years
    i don't have to specify access_token?
  • ifaour
    ifaour about 13 years
    Ah, sorry you are using offline_access then yeah you need to specify the access_token as part of the post array!
  • Anil Bhatt
    Anil Bhatt over 12 years
    @ifaour, very nice post. But my access token getting expire after sometime. Is there any way to make it permanent without using offline_permission? Please Help me?
  • ifaour
    ifaour over 12 years
    @Anil: no, to make an access token permanent then you need the offline_access permission. Again, this permission is not needed for publishing (even if the user is not currently online).
  • Henley
    Henley over 12 years
    This is misleading.. u're saying u dun need offline permission to post to wall anytime. Correct. But when access token expires.. you can't post, correct? So it's not anytime... not heaven =(
  • ifaour
    ifaour over 12 years
    @Hisoka, It depends on what do you mean by "expires". 1) when publishing you don't need to append the access_token at all 2) and just to let you know, even with offline_access, tokens may expire (e.g.: if the user changes his password)
  • lulalala
    lulalala over 11 years
    @ifaour Since there is no need of access_token, how does Facebook know you are your app? Do we need to pass something like app_id?

Related