Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change facebook redirect URL on login

Tags:

php

facebook

I'm trying to change the redirect URL for Facebook login on my site. This is so I can go to a page where I can create a new user in my database if they don't already exist and THEN redirect them to my main page. However, when I try to login, I get the following error on Facebook: An error occurred with PHP SDK Unit Tests. Please try again later.

My code (I want to redirect them to mysite.com/createfbuser.php):

public function getLoginUrl($params=array()) {
     $this->establishCSRFTokenState();
     $currentUrl = $_SERVER['SERVER_NAME'] . '/createfbuser.php';
     return $this->getUrl(
       'www',
       'dialog/oauth',
       array_merge(array(
                'client_id' => $this->getAppId(),
                'redirect_uri' => $currentUrl, // possibly overwritten
                'state' => $this->state,
                'scope' => 'email'),
       $params));
}

EDIT The original code reads $currentUrl = $this->getCurrentUrl(); for a reference

like image 993
tnw Avatar asked Jul 21 '11 18:07

tnw


2 Answers

First of all, you don't have to edit PHP SDK, below is the sample for authenticating the user and then redirecting to your landing page,

Make sure you replace:

YOUR-APP-ID-HERE with Your facebook application id,

YOUR-APP-API-SECRET-HERE with Your facebook application secret key

YOUR-REDIRECT-URL-HERE with Your landing page URL

<?php

    // Requires Facebook PHP SDK 3.0.1: https://github.com/facebook/php-sdk/
    require ('facebook.php');

    define('FACEBOOK_APP_ID',"YOUR-APP-ID-HERE");
    define('FACEBOOK_SECRET',"YOUR-APP-API-SECRET-HERE");
    define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
    $user = null;

    $facebook = new Facebook(array(
        'appId' => FACEBOOK_APP_ID,
        'secret' => FACEBOOK_SECRET,
        'cookie' => true
    ));

    $user = $facebook->getUser(); // Get the UID of the connected user, or 0 if the Facebook user is not connected.

    if($user == 0) {
        // If the user is not connected to your application, redirect the user to authentication page
        /**
         * Get a Login URL for use with redirects. By default, full page redirect is
         * assumed. If you are using the generated URL with a window.open() call in
         * JavaScript, you can pass in display=popup as part of the $params.
         * 
         * The parameters:
         * - redirect_uri: the url to go to after a successful login
         * - scope: comma separated list of requested extended perms
         */

        $login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI));

        echo ("<script> top.location.href='".$login_url."'</script>");

    } else {
        // if the user is already connected, then redirect them to landing page or show some content
        echo ("<script> window.location.href='".REDIRECT_URI."'</script>");
    }

?>

If you want to get extended permissions, then simply add another "scope" parameter to the login url, ex:

$login_url = $facebook->getLoginUrl($params = array('redirect_uri' => REDIRECT_URI,'scope' => 'comma-separated-list-of-requested-extended-perms'));

For more details on permissions, refer facebook permissions docs

like image 145
Syed I.R. Avatar answered Sep 29 '22 07:09

Syed I.R.


The URL you redirect to needs to be on the same domain as you've configured for your app in the app settings, beyond that there's no restriction really, you can set the redirect_url to any URL on your domain.

When you try to auth the app as an admin there should be a more specific error message visible - it's quite likely Error 191 ( see Facebook API error 191 for another possible cause and solution)

like image 23
Igy Avatar answered Sep 29 '22 07:09

Igy