Search code examples
facebookfacebook-graph-apifacebook-php-sdk

Re-asking for Facebook Permissions PHP SDK 5.0


I'm generating a Login URL, which works great, but in my case the email permission is mandatory so I need to explain to the user and re-ask the email permission.

Here's how I'm generating the URL:

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

$helper = $fb->getRedirectLoginHelper();

$permissions = ['email', 'user_birthday', 'user_location', 'user_hometown', 'user_relationships']; // Optional permissions

$loginUrl = $helper->getLoginUrl('http://localhost/urbanportal/wifilogin.php?origlink='.$_SESSION['origlink'].'&routerlink='.$_SESSION['routerlink'].'&siteid='.$_SESSION['siteid'], $permissions);

I read that you can pass a third parameter that says you're doing a re-request like so:

$loginUrl = $helper->getLoginUrl('http://localhost/urbanportal/wifilogin.php?origlink='.$_SESSION['origlink'].'&routerlink='.$_SESSION['routerlink'].'&siteid='.$_SESSION['siteid'], $permissions, true);

But I'm getting a "Length of param app_id must be less than or equal to 32" Facebook error, maybe that was for SDK 4.0?

The documentation always seems to navigate me to:

    FB.login(
  function(response) {
    console.log(response);
  },
  {
    scope: 'user_likes',
    auth_type: 'rerequest'
  }
);

But this is for Javascript SDK...


Solution

  • A quick look in the source code of the SDK reveals that there’s a dedicated method getReRequestUrl for that:

    /**
     * Returns the URL to send the user in order to login to Facebook with permission(s) to be re-asked.
     *
     * @param string $redirectUrl The URL Facebook should redirect users to after login.
     * @param array  $scope       List of permissions to request during login.
     * @param string $separator   The separator to use in http_build_query().
     *
     * @return string
     */
    public function getReRequestUrl($redirectUrl, array $scope = [], $separator = '&')
    

    https://github.com/facebook/facebook-php-sdk-v4/blob/5.0-dev/src/Facebook/Helpers/FacebookRedirectLoginHelper.php#L202


    I hope you are aware though that you will not get an email address for every FB user; f.i. they simply might not have one on file with Facebook, so even with the email permission you won’t get an email address.