且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我的WebApp(使用Facebook登录)登录我

更新时间:2023-11-27 23:34:34

您应该查看延伸Facebook访问令牌



https://developers.facebook.com/docs/ howtos / login / extend-tokens /



然而,这使用了应用程序秘密,所以不应该在javascript中完成,而是在服务器端的语言中通过将您的代码与php-sdk相结合)


I use the facebook Login on my website

window.fbAsyncInit = function() {

    // init the FB JS SDK
    FB.init({
      appId      : 'XXX', // App ID from the App Dashboard
      channelUrl : 'XXX/channel.php', // Channel File for x-domain communication
      cookie     : true, // set sessions cookies to allow your server to access the session?
      frictionlessRequests: true
    });

};

The problem is, I get logged out on my website every ~10-20min. The site it than reloading and I'm logged in again. I think the cookies gets deleted somehow.

Is there a way to make that cookie exist longer?

My login looks like that:

FB.login(function(response) {}, {scope:'publish_actions'});

Update:

This part of my script is called when a user logs in the first time:

// triggers on first login
if (isset($_GET['first_login'])) {
    if ($me) {
        $facebook->setExtendedAccessToken();
        $friends = $facebook->api('/me/friends');
        $friends = $friends['data'];
        if (!empty($friends)) {
            // sql

        }
        $permissions = $facebook->api("/me/permissions");

        if( array_key_exists('publish_actions', $permissions['data'][0]) ) {
            $attachment = array(
                    'message' => 'XXX',
                    'name' => 'XXX - App',
                    'link' => 'XXX',
                    'description' => 'XXX',
                    'picture'=> 'XXX',
                    'access_token' =>  $facebook->getAccessToken()
            );
            $facebook->api('/'.$facebook->getUser().'/feed', 'POST', $attachment);

            // sql
        }
        header('Location: http:/xxx');
    }
}

But I get an error

Fatal error: Uncaught OAuthException: (#1) An error occured while creating the share thrown in /facebook/base_facebook.php on line 1254

Which is caused by $facebook->api('/'.$facebook->getUser().'/feed', 'POST', $attachment);

When I remove the link-attribute from my $attachment, it works. Is the link attribute forbidden or something?

You should look into extending facebook access tokens

https://developers.facebook.com/docs/howtos/login/extending-tokens/

however this uses the app secret so should not be done in javascript but in a server side language as well (possibly by combining your code with the php-sdk)