facebookfacebook-graph-apioauthfacebook-oauth

How do I authorize a facebook application? PHP


I have a php web app set up to handle my new facebook application. I am looking at my app at http://apps.facebook.com/myapp and it shows my app in an iframe.

I am trying to get the app to bring up the screen where it requests permission. I can't seem to find a anything that explains the flow. I don't have an issue reading profile details once I create the $fb->loginUrl() link and click through to it, but I want a permission request to load automatically when you go first to the app.


Solution

  • Try this code - It is working code that I use when I create application. I assume from your message that you use PHP SDK

    < ?php
        require_once('facebook.php');
    // Create our Application instance.
    $facebook = new Facebook(array(
      'appId'  => 'YOUR APP ID',
      'secret' => 'YOUR APP SECRET',
      'cookie' => true
    ));
    if ($user = $facebook->getUser()) { //user is logged in and authorized us
        $user = $facebook->getUser();
        $fbme = $facebook->api('/me');
        print "Welcome User ID: " . $user;
        print_r($fbme);
    
    } else { // User has not authorized us or is not logged in
        $params = array(
            'fbconnect'=>0,
                    'canvas'=>1,
            'req_perms'=>'publish_stream,email',
        // For a full list of permissions: http://developers.facebook.com/docs/authentication/permissions
        );
        $loginUrl = $facebook->getLoginUrl($params);
         print "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
    }
    

    Don't forget to test it on new and fresh browser and other account.