Question: How to get the access token of Facebook app via curl

Question

How to get the access token of Facebook app via curl

Answers 0
Added at 2017-01-02 17:01
Tags
Question

I'm trying to get the access token of a Facebook app by calling the callback url via curl.

In the first function, I'm trying to constrcut the login url this way:

if(!session_start()) {
        session_start();
    }

    $fb = new Facebook([
        'app_id'     => '',
        'app_secret' => '',
        'default_graph_version' => 'v2.8',
    ]);

    $helper = $fb->getRedirectLoginHelper();

    $callback = 'http://www.exemple.com/callback';

    $loginUrl = $helper->getLoginUrl($callback);

In the callback page, I'm getting the access token and forcing the page to exit to display it in JSON

  if(!session_start()) {
    session_start();
}

$fb = new Facebook\Facebook([
    'app_id'     => '',
    'app_secret' => '',
    'default_graph_version' => 'v2.8',
]);

$helper = $fb->getRedirectLoginHelper();

try {
    $accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
    // There was an error communicating with Graph
    echo $e->getMessage();
    exit;
}

if (isset($accessToken)) {
    $data['access_token'] = (string) $accessToken;
    $data = json_encode($data);
    die($data);
} elseif ($helper->getError()) {
    exit;
}

Finally, I'm using Curl to call this page and getting the result.

    $ch = curl_init();

    $url = //The loggin url.

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close ($ch);

    return $result;

This's not working for me. I'm trying to figure out what's wrong.

Answers to

How to get the access token of Facebook app via curl

Source Show
◀ Wstecz