以编程方式登录到Facebook并发布到页面(墙) [英] programmatically login to facebook and post to page (wall)

查看:52
本文介绍了以编程方式登录到Facebook并发布到页面(墙)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,我需要它不时将状态更新发布到Facebook页面上.

使用我的个人Facebook帐户,创建了一个应用程序和一个页面.到目前为止,通过将以下代码添加到我的网站,我已经能够以编程方式发布到Page的墙上:

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>';

}

因此,我只是打开我的网站,单击请登录",然后以我自己的身份登录.登录后,它现在可以将状态更新发布到Facebook页面.

显然,这里的问题是我需要登录才能发布.如果其他用户尝试使用其用户帐户登录,则我的网站无法将状态更新发布到Facebook页面,因为我是该应用程序/页面的唯一管理员.

我的问题是,我是否可以通过编程方式将自己登录到Facebook,以便可以自动对页面进行这些状态更新?

对不起,这里是Facebook开发的全部菜鸟.

解决方案

似乎从Facebook脱机时需要更新状态.为此,您需要获得应用程序用户的脱机访问权限,并且需要使Facebook应用程序具有无限"令牌才能访问API,以便您的程序可以在不登录Facebook的情况下随时更新. >

您可能会在这里得到想要的东西

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

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/

这篇关于以编程方式登录到Facebook并发布到页面(墙)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆