【问题标题】:Facebook access_token problemFacebook access_token 问题
【发布时间】:2011-10-10 20:36:30
【问题描述】:

我在我的应用程序中使用了一个请求对话框,用户可以在其中向他们的朋友发送请求。一旦用户发送请求,我将应用程序重定向到它在用户页面上发布的页面。我使用以下代码获取 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 访问令牌签名。

【问题讨论】:

    标签: php facebook access-token


    【解决方案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.
        }
    
    }
    
    ?>
    

    【讨论】:

    • 非常感谢它让我的生活更轻松
    • 没问题的朋友,很高兴它帮助了你:)
    【解决方案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;
    

    (您也可以手动检查)

    编辑

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

    【讨论】:

      【解决方案3】:

      你正在做的是得到application access token

      App Login 允许您为您的 应用程序,例如检索 Insights 数据或批准请求。图形 API 需要应用访问令牌的调用在 API 中明确表示 参考。

      您需要的是user access token 和一些permissions,在您的情况下是publish_stream

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-05
          • 2011-10-16
          • 2011-02-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多