Mark Segal
Mark Segal

Reputation: 5560

Access token on facebook is invalid - even after making it

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)

Upvotes: 1

Views: 236

Answers (1)

C3roe
C3roe

Reputation: 96424

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.

Upvotes: 1

Related Questions