Rick de Graaf
Rick de Graaf

Reputation: 958

More settings for the facebook php API

Maybe I searched completely in the wrong way, and the facebook documentation pretty much sucks in my opinion.

I was wondering, I'm connecting to facebook with the settings below and that works (I'm able to retrieve the profile information of the logged in user allows my application to access his profile.) Are there other options I can set, like the callback url and the expiration (which I want to set to never, so I can save the token in my db and re-use it)?

This is the config now:

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

I'm also connection to twitter and there I'm able to this:

$this->configTwitter = array(
        'callbackUrl' => 'xxxxxxxxxxxx',
        'siteUrl' => 'http://twitter.com/oauth',
        'consumerKey' => 'xxxxxxxxxxxx',
        'consumerSecret' => 'xxxxxxxxxxxx'
    );

Thanks in advance!

Facebook should take a look at Twitter

Upvotes: 1

Views: 1395

Answers (1)

Rick de Graaf
Rick de Graaf

Reputation: 958

After trying a few days, spitting the documentation of the Graph OAuth 2.0 (which doesn't work as good as expected and searching the internet I found a solution which I used to create the following script:

    // Config
    $this->redirectUrl = 'http://www.mywebsite.com/facebook'
    $this->clientId = 'APPLICATION_ID';
    $this->permissions = 'publish_stream,offline_access';

    // Check if a sessions is present 
    if(!$_GET['session'])
    {
        // Authorize application
        header('Location: http://www.facebook.com/connect/uiserver.php?app_id='.$this->clientId.'&next='.$this->redirectUrl.'&perms='.$this->permissions.'&return_session=1&session_version=3&fbconnect=0&canvas=1&legacy_return=1&method=permissions.request');
    }
    else
    {
        $token = json_decode($_GET['session']);
                    echo $token->access_token; // This is the access_token you'll need!

        // Insert token in the database or whatever you want
    }

This piece of code works great and performs the following actions:

  1. Log in if the user isn't logged in to facebook
  2. Asks if the user wants to add the application and grants the requested permissions (in my case: publish_stream and offline_access) in one dialog
  3. returns to the page you specified under redirectUrl with an access_token (and because we requested for offline access this one doesn't expire)

I then store the token in the database so I won't have to request it again

Now you can, if you wish use the facebook code, or you're own code to get the users data, or other information from facebook (be sure you requested the right permissions)

Don't now if this is facebook-valid code, but it works great and I can easily define the 'config' like I wanted....

Upvotes: 2

Related Questions