且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Facebook API:如何在没有登录的情况下发布到自己的应用程序墙

更新时间:2022-11-06 17:55:40

如果你想发布到你自己的应用程序墙所有您需要的都是一个应用程序访问令牌,如果您想在没有登录的情况下发布到用户墙,您还需要该用户的长时间访问令牌,因为您必须要求离线访问权限。

If you want to post to your own Application wall, all you need is an application Access Token, and if you want to publish to an user wall without login, you also need this user long live access token, for that you have to ask for Offline access permission.

要发布到您的应用程序墙:

To publish to your application wall :

1-卷曲此链接以获取应用程序访问令牌:

1- Curl this link to get your application access token :

https://graph.facebook.com/oauth/access_token
client_id = YOUR_APP_ID& client_secret = YOUR_APP_SECRET&
grant_type = client_credentials

https://graph.facebook.com/oauth/access_token? client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET& grant_type=client_credentials

2-发布到没有c的墙

2- Publish to wall without checking for the session

示例:

<?php
require_once 'facebook.php'

//Function to Get Access Token
function get_app_token($appid, $appsecret)
{
$args = array(
'grant_type' => 'client_credentials',
'client_id' => $appid,
'client_secret' => $appsecret
);

$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$data = curl_exec($ch);

return json_encode($data);
}

// Create FB Object Instance
$facebook = new Facebook(array(
    'appId'  => $appid,
    'secret' => $appsecret,
    'cookie' => false,
    ));


//Get App Token
$token = get_app_token($appid, $appsecret);

//Try to Publish on wall or catch the Facebook exception
try {
$attachment = array('message' => '',
            'access_token' => $token,
                    'name' => 'Attachment Name',
                    'caption' => 'Attachment Caption',
                    'link' => 'http://apps.facebook.com/xxxxxx/',
                    'description' => 'Description .....',
                    'picture' => 'http://www.google.com/logo.jpg',
                    'actions' => array(array('name' => 'Action Text', 
                                      'link' => 'http://apps.facebook.com/xxxxxx/'))
                    );

$result = $facebook->api('/'.$appid.'/feed/', 'post', $attachment);
}

//If the post is not published, print error details
catch (FacebookApiException $e) {
echo '<pre>';
print_r($e);
echo '</pre>';
}

请查看本页面的APP LOGIN部分以获取更多信息:
http://developers.facebook.com/docs/authentication/

Check APP LOGIN part in this page for more informations : http://developers.facebook.com/docs/authentication/