casper123
casper123

Reputation: 1766

Open Auth Dialog in Facebook php SDK

I am integration Facebook connect in my website using php SDK.. Everything is working fine except that I want that instead of redirecting to facebook for authentication, I want to open OAuth Dialog.. Currently Im using following url for authentication,.. It redirects to facebook but doesn't open OAuth Dailog..

https://www.facebook.com/dialog/oauth?client_id=XXXXXXXXXXX&redirect_uri=http%3A%2F%2Fwww.setsail2nz.com.au%2Fmicrosite%2Ffbalbums.php%3Fpid%3D1&state=ad89eddd7f71e7337785f604710c97e8&scope=user_photos%2Cpublish_stream%2Cmanage_friendlists%2Cemail&display=popup

Any ideas?

EDIT: I know how to do it with Facebook JS SDK.. But is there a way to do it with php?

Edit 2: Okay now im using JS SDK but still its not opening the dialog.. Here is my code

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
     appId      : 'XXXXXXXXXX', // App ID
     status     : true, // check login status
     cookie     : true, // enable cookies to allow the server to access the session
     xfbml      : true,  // parse XFBML
     oauth   : true // enables OAuth 2.0
    });
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

  function fblogin()
  {
    FB.login(function(response)
    {
        alert(response);

    },{perms: "user_photos,publish_stream,manage_friendlists,email"});
  }
</script>

Upvotes: 1

Views: 2431

Answers (2)

neon
neon

Reputation: 1

To answer your question about detecting the dialog response with PHP SDK:

The problem is that getloginURL only takes one URL parameter that it uses on success or error. I needed to detect if the user clicked OK or Cancel on the permissions dialog, so I could either redirect to a pre-permissions dialog page, or show the correct logged-in content.

All I did was check if 'error_reason' existed in the url parameters, and if the error was 'user_denied', and took appropriate action. There might be a better way, but this worked for me.

<?php
    //Facebook Authentication
    $user = $facebook->getUser();

    //Get Login URL
    $loginUrl = $facebook->getLoginUrl(); 

    //User is logged in
    if ($user) {
      try {
        // Proceed knowing you have a logged in user who's authenticated.
        $user_profile = $facebook->api('/me');
      } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
      }
    }

    //User is not logged in
    if (!$user) {
        //Check if error_reason was generated and if the user denied login
        if (isset($_REQUEST['error_reason']) && ($_REQUEST['error_reason']=='user_denied')) {
            //user cancelled permissions dialog thus not logged in to app, redirect elsewhere
            echo "<script type='text/javascript'>top.location.href = 'YOUR REDIRECT URL';</script>";
            exit;
        } else {
            //user not logged in so initiate permissions dialog
            echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
            exit;
        }
    }

    //get user basic description
    $userInfo = $facebook->api("/$user");
?>

Upvotes: 0

Artjom Kurapov
Artjom Kurapov

Reputation: 6155

Use Javascript SDK https://developers.facebook.com/docs/reference/javascript/

And FB.login, passing your required permissions

Upvotes: 1

Related Questions