dongtaotao19830418 2012-08-08 23:38
浏览 75
已采纳

Facebook上的访问令牌无效 - 即使在制作之后也是如此

I'm writing a facebook application in PHP with the facebook SDK.
The important parts in my code are:

    $facebook = new Facebook(array(
                'appId' => $ApplicationID,
                'secret' => $ApplicationSecret,
                'cookie' => true, // enable optional cookie support  
            ));
    $user = $facebook->getUser();

              $post = $facebook->api('/' . $user . '/feed', 'post', array(
              stuff
      ));
                $_SESSION['finish'] = false;
                $_SESSION['inside'] = 'yes';
                $_SESSION['uid'] = $user;
                $token = $facebook->getAccessToken();
                echo 'Access token: ' .$token;
                $_SESSION['friends'] = $facebook->api('/me/friends?access_token'.$token);

You can see I am echoing the access token.
At first, I haven't added the access_token into the query - I just added it for checking.
The problem is that the last like (with the /me/friends/) throws an exception:

OAuthException: An active access token must be used to query information about the current user.

Although I did a login, and the feed post did work (I checked at my wall, it's there). Then, my try catch block handles the exception by redirecting to the login link, this way:

catch(Exception $e) {

            $login_url_params = array(
            'scope' => 'stuff',
            'fbconnect' => 1,
            'redirect_uri' => $ApplicationURL
        );
        $login_url = $facebook->getLoginUrl($login_url_params);

        //redirect to the login URL on facebook
        echo ("EXCPETION! " . $e . "<script> top.location.href='" . $login_url . "'</script>");
        exit();
        }

The code writing the exception is obviously only for debugging purposes.
Well, the first time I run the application a similar code is executed by a if(!$user) condition. Then it asks for my permission and I give it. Then, again, it says the access token is invalid (but does post to my wall).
Please note that I've compared the access_tokens, and even after removing the application and doing it all again - it stayed the same.

This is very awkward behavior and I fail to understand it. May anyone please shred some light on this?
Edit: For some weird reason, the token doesn't change even after going to a different user. But the post on the wall is still made... (And the exception thrown when accessing the friends list)

  • 写回答

1条回答 默认 最新

  • doubu1853 2012-08-09 12:30
    关注

    For some weird reason, the token doesn't change even after going to a different user. But the post on the wall is still made... (And the exception thrown when accessing the friends list)

    That sounds to me as if you don’t actually have a user currently logged in, so the SDK is just using your app access token, which is always the default token it will use before it receives another, more “specific” one.

    What format does your access token have – does is look something like your_app_id|your_app_secret or your_app_id|some_random_characters? If it’s starting with your app id, it’s most definitively an app access token.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题