More settings for the facebook php API

和自甴很熟 提交于 2020-01-05 19:20:15

问题


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


回答1:


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....




回答2:


There are many settings you can set at the point of authenticating. Extended permissions let you set the offline_access so that you can authenticate and store your session in a db. More info on extended permissions are at developers.facebook.com/docs/authentication/permissions and you can read my blog post for more info on how to use them at http://www.joeyrivera.com/2010/facebook-graph-api-app-easy-w-php-sdk/



来源:https://stackoverflow.com/questions/4366709/more-settings-for-the-facebook-php-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!