2012-07-28 46 views
1

我無法得到以下代碼發佈到用戶名爲514559322的用戶牆上。但是,如果將$ uid的值替換爲字符串'me',我可以發佈到我自己的牆上。由於身份驗證問題,無法發佈到Facebook牆?

<?php 
require_once "../src/facebook.php"; 

$app_id = "my app id"; 
$app_secret = "my app secret"; 
$uid = 514559322; 

// Init facebook api. 
$facebook = new Facebook(array(
     'appId' => $app_id, 
     'secret' => $app_secret, 
     'cookie' => true 
)); 

// Get the url to redirect for login to facebook 
// and request permission to write on the user's wall. 
$login_url = $facebook->getLoginUrl(
    array('scope' => 'publish_stream') 
); 

// If not authenticated, redirect to the facebook login dialog. 
// The $login_url will take care of redirecting back to us 
// after successful login. 
if (! $facebook->getUser()) { 
    echo <<< EOT 
<script type="text/javascript"> 
top.location.href = "$login_url"; 
</script>; 
EOT; 

    exit; 
} 

// Do the wall post. 
$facebook->api("/$uid/feed", "post", array(
    message => "Hello win95", 
    picture => "http://cdn.papyimg.com/wp-content/uploads/2011/03/Windows-95-500x312.png", 
    link => "http://en.wikipedia.org/wiki/Windows_95", 
    name => "Go windows 95", 
    caption => "Caption - this is the best operating system in the world!" 
)); 
?> 

我該如何讓我的PHP腳本發佈uid 514559322的用戶牆?

添加一個嘗試捕捉周圍循環API調用後,這裏是我得到的異常消息:

OAuthException: (#1) An error occured while creating the share 

那麼,在我的代碼是否要提供認證信息?或者適當的授權令牌?

+0

加入'try {[POST TO WALL CODE]} catch(FacebookApiException $ e){exit($ e); }'看到問題.. – 2012-07-28 22:05:19

回答

1

你忘了access_token PARAM:

// Do the wall post. 
$facebook->api("/$uid/feed", 'post', array(
    'access_token' => $facebook->getAccessToken(), 
    'message' => "Hello win95", 
    'picture' => "http://cdn.papyimg.com/wp-content/uploads/2011/03/Windows-95-500x312.png", 
    'link' => "http://en.wikipedia.org/wiki/Windows_95", 
    'name' => "Go windows 95", 
    'caption' => "Caption - this is the best operating system in the world!" 
)); 

或者,您需要offline_access權限,這是不好的,因爲很快就會棄用。

1

您的用戶需要授權您的應用程序使用「offline_access,publish_stream」(儘管現在正在替換,請參閱http://developers.facebook.com/roadmap/offline-access-removal/)。

然後,您可以在API調用注入令牌

$res = $facebook->api('/me/feed', 'POST', $post); 

其中$崗位是$的access_token一個陣列和$消息