WordPress and Facebook SDK cross-site request errors

  • Unknown's avatar

    Hello,

    I’m using Facebook SDK on casinobonusca.com website, trying to get the Facebook user ID:

    <code class="hljs language-php" style="margin-right: 0px; margin-bottom: 0px; margin-left: 0px; border-width: 0px; border-style: initial; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-size: var(--fs-body1); vertical-align: baseline; box-sizing: inherit; white-space: inherit;">$fb = new FacebookFacebook([
        'app_id' => '11111111111',
        'app_secret' => '1111222211111112222',
        'default_graph_version' => 'v2.4',
    ]);
    
    $helper = $fb->getRedirectLoginHelper();
    
    
    $permissions = ['public_profile','email']; // Optional permissions
    $loginUrl = $helper->getLoginUrl('https://casinobonusca.com', $permissions);
    
    echo ' . $loginUrl . '">Log in with Facebook!';
    
        try {
            $accessToken = $helper->getAccessToken();
            var_dump($accessToken);
        } catch (FacebookExceptionsFacebookResponseException $e) {
            // When Graph returns an error
            echo 'Graph returned an error: ' . $e->getMessage();
            exit;
        } catch (FacebookExceptionsFacebookSDKException $e) {
            // When validation fails or other local issues
            echo 'Facebook SDK returned an error: ' . $e->getMessage();
            exit;
        }
    
        if (!isset($accessToken)) {
            if ($helper->getError()) {
                header('HTTP/1.0 401 Unauthorized');
                echo "Error: " . $helper->getError() . "n";
                echo "Error Code: " . $helper->getErrorCode() . "n";
                echo "Error Reason: " . $helper->getErrorReason() . "n";
                echo "Error Description: " . $helper->getErrorDescription() . "n";
            } else {
                header('HTTP/1.0 400 Bad Request');
                echo 'Bad request';
            }
            exit;
        }
    
    // Logged in
        echo '<h3>Access Token</h3>';
        var_dump($accessToken->getValue());
    
    // The OAuth 2.0 client handler helps us manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();
    
    // Get the access token metadata from /debug_token
        $tokenMetadata = $oAuth2Client->debugToken($accessToken);
        echo '<h3>Metadata</h3>';
        var_dump($tokenMetadata);
    
    // Validation (these will throw FacebookSDKException's when they fail)
        $tokenMetadata->validateAppId($config['11111111111']);
    // If you know the user ID this access token belongs to, you can validate it here
    //$tokenMetadata->validateUserId('123');
        $tokenMetadata->validateExpiration();
    
        if (!$accessToken->isLongLived()) {
            // Exchanges a short-lived access token for a long-lived one
            try {
                $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
            } catch (FacebookExceptionsFacebookSDKException $e) {
                echo "

    Error getting long-lived access token: "

    . $helper->getMessage() . "nn"; exit; } echo '<h3>Long-lived</h3>'; var_dump($accessToken->getValue()); } $_SESSION['fb_access_token'] = (string)$accessToken;
    I always get the response: The "state" param from the URL and session do not match. I think the problem is related to CSRF protection. Any assistance you can provide would be greatly appreciated. Thank you.
  • Hi there,

    https://casinobonusca.com appears to use the WordPress software but is hosted with another host than WordPress.com. In order to get help with your website, we do suggest contacting either your host’s own customer support, or alternatively the community forums of WordPress.org:

    To clarify, there are two ways to use WordPress, you can either use the WordPress software downloaded from WordPress.org onto a web host (also known as self-hosted), or you may host your site with us here at WordPress.com. For reference, you can learn more about the differences between WordPress.com and WordPress.org here:
    https://support.wordpress.com/com-vs-org/

  • The topic ‘WordPress and Facebook SDK cross-site request errors’ is closed to new replies.