1

我在我的应用程序中使用了一个请求对话框,用户可以在其中向他们的朋友发送请求。一旦用户发送请求,我将应用程序重定向到它在用户页面上发布的页面。我使用以下代码获取 access_token:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/oauth/access_token?client_id='.APP_ID.'&client_secret='.APP_SECRET.'&redirect_uri=http://www.facebook.com/pages/Cosmetics/167231286678063?sk=app_233227910028874&grant_type=client_credentials');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);

$token = curl_exec($ch);

$me = $facebook->api('/me?'.$token);

但是当我尝试在墙上发帖时,它会显示此错误:

致命错误:未捕获的 OAuthException:无效的 OAuth 访问令牌签名。

4

4 回答 4

2

我不确定您究竟是如何尝试进行身份验证的。以这种方式提出请求可能没问题,但我使用的方法是此处记录的方法:http: //developers.facebook.com/docs/authentication/

也就是说,您首先将用户重定向到 Facebook 页面,以使他接受权限。一旦他接受,他就会被重定向到您提供的 URL,code=###您需要使用 get 参数来执行服务器端请求,以获得您需要的真实 access_token。

重定向网址示例:

$my_url = 'http://www.example.com';
"http://www.facebook.com/dialog/oauth?client_id=". $app_id ."&redirect_uri=". urlencode($my_url);

然后,当用户点击接受时,他会被重定向到,例如

http://www.example.com?code=ABCDEF

然后,在服务器端,您需要获取 access_token 进行如下调用:

"https://graph.facebook.com/oauth/access_token?"
   . "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url)
   . "&client_secret=" . $app_secret . "&code=" . $code;

然后仔细检查该调用返回的文本,并尝试获取

"https://graph.facebook.com/me?access_token=". $access_token;

(您也可以手动检查)

编辑

此外,您可能需要用户的更多权限才能在他的流上发布。因此,请求scope=publish_stream向第一个(用户授权)URL 添加 get 的权限。

于 2011-07-22T08:17:32.430 回答
1

您正在使用&grant_type=client_credentials它为您的整个应用程序提供访问令牌,而不是为您当前的用户。要为您当前的用户获取访问令牌,您需要将它们重定向到相同的 url,但没有&grant_type=client_credentials然后 Facebook 会将它们重定向回您redirect_uri并发送您需要的 access_token。或者,您可以使用 Javascript SDK 通过弹出窗口获取访问令牌,而无需重定向它们。

这是另一个关于如何使用 JS SDK 进行身份验证且没有重定向的问题的链接:Facebook Authentication without Redirect?

于 2011-07-22T08:14:22.680 回答
1

为什么不使用 PHP SDK 3.0.1 ?

如果您使用 facebook 提供的 sdk,这很容易,这是使用 php sdk 3.0.1 进行身份验证的示例:

<?php

// Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
require ('facebook.php');

define('FACEBOOK_APP_ID',"YOUR-APP-ID-HERE");
define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE");
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
define('PERMISSIONS_REQUIRED', "publish_stream,user_photos");
$user = null;

$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true
));

$user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.

if($user == 0) {
    // If the user is not connected to your application, redirect the user to authentication page
    /**
     * Get a Login URL for use with redirects. By default, full page redirect is
     * assumed. If you are using the generated URL with a window.open() call in
     * JavaScript, you can pass in display=popup as part of the $params.
     * 
     * The parameters:
     * - redirect_uri: the url to go to after a successful login
     * - scope: comma separated list of requested extended perms
     */

    $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));

    echo ("<script> top.location.href='".$login_url."'</script>");

} else {
    // if the user is already connected, then fetch access_token and user's information or show some content to logged in user.
    try
    {
        $access_token = $facebook->getAccessToken(); // Gives you current user's access_token

        $user = $facebook->api('/me'); // Gets User's information based on permissions the user has granted to your application.

    } catch(FacebookApiException $e){
        $results = $e->getResult();
        // Print results if you want to debug.
    }

}

?>
于 2011-07-22T21:23:11.753 回答
0

你在那里做的是获取应用程序访问令牌

应用登录允许您对应用执行各种管理操作,例如检索洞察数据或批准请求。API 参考中明确指出了需要应用访问令牌的图形 API 调用。

您需要的是具有某些权限的用户访问令牌,在您的情况下它会是。publish_stream

于 2011-07-22T08:16:19.450 回答