10

我想将带有脚本的文本发布到我自己的应用程序墙上,但无需先登录,因为它应该自动完成。我怎么能那样做?我已经试过了:

$fb = new Facebook(array(
    'appId'  => 'appid',
    'secret' => 'appsecret',
    'cookie' => true
));


if ($fb->getSession()) {
    // Post
} else {
    // Logger
    // Every time I get in here :(
}

我必须做什么才能使用脚本访问我自己的应用程序墙?

4

2 回答 2

13

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

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

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

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

2- 发布到墙上而不检查会话

例子 :

<?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/

于 2011-03-05T18:24:25.263 回答
3

我不能将此作为评论,因为我没有要点,但如果有人有类似的问题 - 如果 McSharks 的答案不起作用,请删除 json_encode 作为其编码引号,它应该可以工作。

于 2012-01-31T10:30:53.803 回答