【问题标题】:Authenticating a FACEBOOK Application - PHP验证 FACEBOOK 应用程序 - PHP
【发布时间】:2026-01-04 14:30:01
【问题描述】:

我需要在发布到我自己的墙上之前进行身份验证,所以这是我的代码

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 访问令牌签名。

【问题讨论】:

  • 我在上面发布的链接将帮助您入门。您正在请求“应用程序访问令牌”!这不是用来代表用户执行任务的!

标签: php facebook facebook-php-sdk


【解决方案1】:

您可以直接从 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

并请求您需要的权限。

【讨论】:

    【解决方案2】:

    $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/'))
                  );
    

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

    【讨论】:

    【解决方案3】:

    client_credentials 访问令牌不授予对 /me 的任何访问权限(在这种情况下,“我”是谁?没有任何东西可以将其与用户联系起来),更不用说发布访问权限了。它仅用于应用程序级 Graph API 调用,例如获取应用程序的 Insights 数据。

    【讨论】:

    • 你能帮我解决这个问题吗?
    • 您需要完全重写您的 oauth 流程。
    • 我是 FB 应用程序的新手,我今天才开始,所以我需要代码:|
    • 文档 (developers.facebook.com/docs/authentication) 应该相当容易理解。这不是一个真正的免费代码网站——我通常是通过编写代码获得报酬的。
    • 你能写一个小代码,在墙上贴一个废话吗?