2011-04-07 83 views
0

我需要张贴到我自己的墙壁前进行身份验证,所以这里是我的代码认证Facebook应用程序 - PHP

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); 
} 

$token = get_app_token($appid, $appsecret); 
$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('/me/feed/', 'post', $attachment); 

我得到错误OAuthException ,无效的OAuth访问令牌签名。

+0

的[需要对张贴在墙上用户帮助]可能重复(http://stackoverflow.com/questions/5126665/need-帮助发布在用户墙上) – ifaour 2011-04-08 11:20:58

+0

我上面发布的链接将帮助您入门。您正在申请一个“应用程序访问令牌”!这不是用来代表用户完成任务的! – ifaour 2011-04-08 11:21:53

回答

0

您可以直接从facebook对象的会话数组中获取access_token。

$session = $facebook->getSession(); 
$session['access_token']; 

当然你需要被授权。如果没有设置会话,您必须重定向到loginUrl

$facebook->getLoginUrl(array('req_perms' => 'email,read_stream,publish_stream')); 
// this will return url for your authorization 

和您需要的请求权限。

+0

如果我告诉你所有的代码你可以修复它吗? – Sourav 2011-04-07 13:18:26

-1

A client_credentials访问令牌不给任何访问/me(谁会“我”在这种情况下?没有任何关系到用户),更不用说张贴访问权限。它仅用于应用程序级图形API调用,例如获取应用程序的Insights数据。

+0

你能帮我解决这个问题吗? – Sourav 2011-04-07 14:05:21

+0

你需要完全重写你的oauth流。 – ceejayoz 2011-04-07 14:14:00

+0

我很新的FB应用程序,我今天刚刚开始,所以我需要代码:| – Sourav 2011-04-07 14:18:32

0

$token价值为access_token=<YOUR_ACCESS_TOKEN>

你不能直接传递$token价值的access_token参数。

$token = get_app_token($appid, $appsecret); 
$access_token = split('=',$token); 
$attachment = array(
        'message' => '', 
        'access_token' => $access_token[1], 
        '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/')) 
      ); 

希望这将有助于使这项工作... :)

+0

我很感谢你的帮助,但它返回了同样的错误,请看看我的代码http://www.web-shine.in/tutorials/fb/code.txt – Sourav 2011-04-08 03:02:14

+0

'$ token'的值是多少? – 2011-04-08 05:52:00