2011-03-05 84 views
10

我要张贴到我自己的应用程序墙上的文字与脚本,但没有先登录,因为它应该自动完成。我怎么能这样做?我已经试过了:Facebook的API:如何张贴到自己的应用程序墙没有登录

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


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

我该怎么做才能使用脚本访问我的应用程序墙?

+0

无需登录到fb我不认为你可以访问它的任何功能。 – 2011-03-05 17:52:19

+0

好的,我怎么能发布到应用程序墙? – Poru 2011-03-05 17:56:25

回答

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/

+0

我试图实现这一点,但它说“不支持的发布请求”,你知道为什么吗? – 2015-11-10 18:54:40

+0

这不再有效。目前的api没有'api'方法,而是'post'。 – ProfK 2016-09-23 04:22:13

3

我不能离开这个作为评论,因为我没有这个点,但如果任何人有类似的问题 - 如果McSharks回答不工作,删除json_encode作为其编码引号,它应该工作。

相关问题