简体   繁体   中英

programmatically login to facebook and post to page (wall)

I have a website, and I need it to post status updates to a Facebook Page from time to time.

Using my personal Facebook account, I created an App, and a Page. So far, I've been able to programmatically post to my Page's Wall, by adding this code to my website:

include_once "lib/facebook/src/facebook.php";

$facebook = new Facebook(array('appId' => 'APP_ID_HERE', 'secret' => 'APP_SECRET_HERE'));

if($facebook->getUser()) {

    try {
        $ret_obj = $facebook->api('/FACEBOOK_PAGE_ID_HERE/feed', 'POST', array(
            'link' => 'www.example.com',
            'message' => 'Posting with the PHP SDK!',
            'access_token' => 'FACEBOOK_PAGE_ACCESS_TOKEN_HERE'
        ));
        echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';

    } catch(FacebookApiException $e) {
      // user logged out (has user_id, but invalid access token)
      $login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream')); 
      echo 'Please <a href="' . $login_url . '">login.</a>';
    }
    echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout</a>';

} else {

    $login_url = $facebook->getLoginUrl(array('scope' => 'publish_stream'));
    echo 'Please <a href="' . $login_url . '">login.</a>';

}

So I just open my website, click "Please login", login as myself. Once I'm logged it, it will now be able to post the status update to the Facebook Page.

Obviously, the problem here is that I need to be logged in for it to be able to post. If other users will try to login with their user accounts, my website cannot post status updates to the Facebook Page because I am the only Admin for the app/Page.

My question is, is there a way for me to programmatically log myself into Facebook so I can do these status updates to my Page automatically?

Sorry, total noob here to Facebook development.

Seems like you need to update status while you are offline from facebook. To do this you need to get the offline access permission of the app user and you need to have a "infinite" token for facebook app to access the APIs, so that your program can update anytime you want with out logging into facebook.

You may get something you want here

http://developers.facebook.com/docs/authentication/

I'm a little late to the party here, but I thought I'd post my gist example for how to do something very similar. I added some functionality to fbconsole to make it easy to programmatically login with fbconsole.automatically_authenticate to make it much easier to access this information in a systematic way. This addition has not yet been incorporated into the master branch of fbconsole (it was just posted this morning), but it is available here in the meantime for those that are interested.

This is what I use to do,

 // = SET WALL DATA HERE ===============
$attachment = array(
'access_token' => 'My Access token here',
'message'      => '',
'name'         => 'My Wall Post Header/Title Here',
'caption'      => 'Small caption here',
'link'         => 'http://www.mywebsite.org',
'description'  => 'Wall Post Details Here',
'picture'      => "http://www.mywebsite.org/images/logo.gif",
);

// =POST ON FACEBOOK WALL ==========================
$this->facebook->api('/me/feed', 'POST', $attachment);
, $attachment);

So include your Facebook API, Get your Facebook Access token, SET data for your wall post and call for facebook api with me/feed method.

This will make a wall post on your wall without asking for login.

make sure you are using correct Access Token.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM