phpfacebookauthenticationfacebook-opengraph

Facebook graph login - website integration, failed get token


I have read about facebook opengraph connection - as far as I have understood, the login procedure is made within 3 steps:

  1. Get the login url from the facebook api call in order to create a custom login button Upon clicking on the link we are redirected to the facebook login page

  2. Do the actual login and get redirected to the site we have defined in the app facebook developer page

  3. In this page we have to deal with the actual result. Here comes the problem: I've understood that we have to use the token and make a new request to the fb page in order to validate the token (see below).

After this, I assume we get the user actual details but I never manage to execute this token request and therefore I'm not sure in how to proceed.

$ch = curl_init("http://graph.facebook.com/oauth/access_token?    client_id=".$facebook_config['appId']."    
                                                          &client_secret=".$facebook_config['secret']."
                                                          &redirect_uri=".urlencode(SITE_DOMAIN)."/facebook_login_processor.php
                                                          &code=".$_GET['code']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 30000);
$data = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);

if ($curl_errno > 0) 
{
    echo "cURL Error ($curl_errno): $curl_error\n";
} 
else 
{
    echo $data;
}

I'm getting an error regarding the redirect_uri - somehow it is not getting validated.

{"error":{"message":"Missing redirect_uri parameter.","type":"OAuthException","code":191}}

Bear in mind that I'm trying this on a non public website (domain is only registred in the server and in my hosts file). Does this impose a problem?

I appreciate any help and thank you for your attention and help

Ricardo

Ps.: sorry about any error - I'm on the phone


Solution

  • Based on facebook php sdk, the script below simply works well on my website.

        <?php
        require_once "lib/facebook.php";
    
        $scope = "create_note,email friends_groups,friends_interests";
        $redirecturl = "https://www.mysite.com/callback.php";
    
        $facebook = new Facebook(array('appId' => APP_ID,'secret' => APP_SECRET));
        $authurl = $facebook->getLoginUrl( array( 'scope' => $scope, 'redirect_uri' => $redirecturl );
    
        header("Location:$authurl");
    
        ?>
    

    then on callback.php

        <?php
        require_once "lib/facebook.php";
    
        $facebook = new Facebook(array('appId' => APP_ID,'secret' => APP_SECRET));
        $user = $facebook->getUser();
        if ($user) {
          try {
            $user_profile = $facebook->api('/me');
          } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
          }
    
          if (!empty($user_profile)) {
             print_r($user_profile);
             // or do something here
          }
        }
        ?>
    

    Note: You should define APP_ID and APP_SECRET.