简体   繁体   中英

Can't get the access token in facebook sdk 5 login using php?

I hosted my files in heroku hosting. Facebook login callback file throws both the exceptions.

First exception

Response Exception: Can't Load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.

Second Exception after reloading

SDK Exception: Cross-site request forgery validation failed. Required param "state" missing from persistent data.

fb-callback.php

<?php
    require_once "config.php";
    try {

        $accessToken = $helper->getAccessToken();
    } catch (\Facebook\Exceptions\FacebookResponseException $e) {
        echo "Response Exception: " . $e->getMessage();
        exit();
    } catch (\Facebook\Exceptions\FacebookSDKException $e) {
        echo "SDK Exception: " . $e->getMessage();
        exit();
    }

    if (!$accessToken) {
        header('Location: login.php');
        exit();
    }

    $oAuth2Client = $FB->getOAuth2Client();
    if (!$accessToken->isLongLived())
        $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);

    $response = $FB->get("/me?fields=id,name,email", $accessToken);
    $userData = $response->getGraphNode()->asArray();
    $_SESSION['userData'] = $userData;
    $_SESSION['access_token'] = (string) $accessToken;
    header('Location: index.php');
    exit();
?>

I tried removing the try catch block and just used the statement

$accessToken = $helper->getAccessToken();

This caused error 500

This page isn't working

fanism.herokuapp.com is currently unable to handle this request. HTTP ERROR 500

config.php (used in fb-callback.php)

<?php
    session_start();

    require_once "Facebook/autoload.php";

    $FB = new \Facebook\Facebook([
        'app_id' => '952208124958752',
        'app_secret' => '9088c861803fda4dadbe950915ae14f2',
        'default_graph_version' => 'v2.10'
    ]);

    $helper = $FB->getRedirectLoginHelper();
?>

created composer.json file in the project folder with the following code

{
  "require" : {
    "facebook/graph-sdk" : "~5.0"
  }
}

run command in project folder(command prompt)

composer install

facebook sdk folder( vendor ) download and from that vendor folder, take out the Facebook folder and paste it in project.(deleted the previous manually downloaded Facebook folder)

then I uploaded that files normally to Heroku. Finally solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM