Facebook 应用 : OAuthException: An active access token must be used

标签 facebook sdk

我在将我的旧 Facebook 应用程序迁移到 Oauth 2.0 时遇到下一个问题:

当我尝试读取用户配置文件(在 php 代码中)时,出现错误:“OAuthException:必须使用事件访问 token 来查询有关当前用户的信息。

应用程序通过 javascript 进行登录操作,然后重定向到一个页面,我从 php 获取用户配置文件。

JavaScript 代码:

<script type="text/javascript">
window.fbAsyncInit = function() {   
    FB.init({
        appId   : '<?php echo $facebook->getAppId() ?>',
        cookie  : true, // enable cookies to allow the server to access the session
        xfbml   : true, // parse XFBML
        oauth : true    // enable oauth
    });

    // whenever the user logs in, we refresh the page
    FB.Event.subscribe('auth.login', function(response) {
        window.location="index.php";
    });

};

(function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
}());


function login () {
 FB.login(function(response) {
   if (response.authResponse) {
     //console.log('Welcome!  Fetching your information.... ');
     FB.api('/me', function(response) {
       //alert('Good to see you, ' + response.name + '.');
       var url="./code.php";
       window.location=url;
     });
   } else {
     alert("Debes de identificarte y aceptar las condiciones para obtener el descuento");
   }
 }, {scope: 'email'});

};


</script>

php 部分(在 code.php 中):

require 'facebook-php-sdk/src/facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => 'xxxxxxxxxxxxxxxxx',
  'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
));
$user = $facebook->getUser();   
if ($user) {
    try {
        // Proceed knowing you have a logged in user who's authenticated.
        $me = $facebook->api('/me');
.....

我阅读了不同的帖子并尝试了一些操作,例如从 javascript 登录响应或 cookie 中获取 access_token,然后调用 api('me/?access_token=...),但它没有用。

编辑:非常感谢大家,现在可以使用了!!

谢谢

最佳答案

@moguzalp @brandwaffle @yauros,在非 Canvas 应用程序上获得 signed_request。我使用了以下代码,它对我有用。 如果您使用的是 Oauth 2.0,那么您将获得名为 fbsr_app_id 的 cookie。此 cookie 只不过是一个 signed_request。

您可以获取此cookie并解析signed_request以获取用户id和access_token,如下所示:

$cookieName = 'fbsr_' . $app_id;

function parse_signed_request($signed_request, $secret) {
  list($encoded_sig, $payload) = explode('.', $signed_request, 2); 

  // decode the data
  $sig = base64_url_decode($encoded_sig);
  $data = json_decode(base64_url_decode($payload), true);

  if (strtoupper($data['algorithm']) !== 'HMAC-SHA256') {
    error_log('Unknown algorithm. Expected HMAC-SHA256');
    return null;
  }

  // check sig
  $expected_sig = hash_hmac('sha256', $payload, $secret, $raw = true);
  if ($sig !== $expected_sig) {
    error_log('Bad Signed JSON signature!');
    return null;
  }

  return $data;
}
function base64_url_decode($input) {
  return base64_decode(strtr($input, '-_', '+/'));
}
$fbsr=parse_signed_request($_COOKIE[$cookieName],$application_secret);

$user_id=$fbsr['user_id'];

关于 Facebook 应用 : OAuthException: An active access token must be used,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8687473/

相关文章:

ios - 如何以编程方式将视频从 iPhone 应用程序上传到 youtube?

java - 7.1 的黑莓应用程序兼容性

ios - Deezer SDK IOS 按 id 播放轨道

java - unity : Resolving Android Dependencies freeze. 无法导入任何插件

iphone - 使用 SocialFramework 登录 Facebook

javascript - 如果用户已经喜欢任何 Facebook 页面,则捕获事件

ios - Facebook ios SDK - 如何处理提要对话框回调?

css - 无法删除 Facebook 按钮后面的白色背景

iphone - 我的 MPMoviePlayerController [iPhone SDK] 有问题

android - adt eclipse 包 02-07-2014 更新