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.

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

报告相同问题?

悬赏问题

  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭