2011-03-05 83 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作爲其編碼引號,它應該工作。

相關問題