来自应用程序ID和秘密的Facebook cookie

时间:2014-06-01 09:24:25

标签: php wordpress facebook-graph-api

我正在使用当前WordPress项目中的Facebook API为我的WordPress网站提供登录功能。我使用以下代码从Facebook app id和secret获取cookie。

$args = array();
    parse_str(trim($_COOKIE['fbs_' . $app_id], ''), $args);
    ksort($args);
    $payload = '';
    foreach ($args as $key => $value) {
      if ($key != 'sig') {
        $payload .= $key . '=' . $value;
      }
    }
    if (md5($payload . $application_secret) != $args['sig']) {
      return null;
    }
    var_dump($args);
    return $args;

但根据link1link2,代码已弃用。所以我使用以下代码来获取cookie。

function get_facebook_cookie($app_id, $application_secret) {

    $data = array();

    if (isset($_COOKIE['fbsr_' . $app_id])) {
        if (list($encoded_sig, $payload) = explode('.', $_COOKIE['fbsr_' . $app_id], 2)) {
            $sig = $this->base64_url_decode($encoded_sig);
            if (hash_hmac('sha256', $payload, $application_secret, true) == $sig) {
                $data = json_decode($this->base64_url_decode($payload), true);
                return $data;
            }                
        }
    } else {
        print 'error';
        return null;
    }
}

function base64_url_decode($input) {
    return base64_decode(strtr($input, '-_', '+/'));
}
顺便说一下,我调用方法的主要方法是

function profile_manager_facebook() {
    global $wpdb;
    $fb_data = get_option('fb_settings', array());
    $permissions = array();
    $permissions = isset($fb_data['permissions']) ? $fb_data['permissions'] : '';
    $size = __('medium', 'profile-manager');
    $login_text = __('Login', 'profile-manager');
    $logout_text = __('Logout', 'profile-manager');
    $connect_text = __('Connect', 'profile-manager');
    $cookie = $this->get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
    print_r($cookie);
    $_SESSION['fb_cookie'] = $cookie;
    $perms = apply_filters('fb_connect_perms', $permissions);
    //only show facebook connect when user is not logged in
    if (is_user_logged_in()) {
        if (!empty($_SESSION['fb_cookie'])) {
            do_action('fb_connect_button_fb_wp');
            ?>
            <a class="fb_button fb_button_<?php echo $size; ?>" href="<?php echo wp_logout_url(get_bloginfo('url')); ?>">
                <span class="fb_button_text">
                    <?php echo $logout_text; ?>
                </span>
            </a>
            <?php
        } else {
            do_action('fb_connect_button_nofb_wp');
            ?>
            <fb:login-button data-scope="<?php echo implode(',', $perms); ?>" data-size="<?php echo $size; ?>" >
                <?php echo $connect_text; ?>
            </fb:login-button>
            <?php
        }
    } else {
        if (!empty($_SESSION['fb_cookie'])) {
            do_action('fb_connect_button_fb_nowp');
            _e('Facebook Connect error: login process failed!', 'wp-facebook-connect');
        } else {
            do_action('fb_connect_button_nofb_nowp');
            ?>
            <fb:login-button data-scope="<?php echo implode(',', $perms); ?>" data-size="<?php echo $size; ?>" >
                <?php echo $login_text; ?>
            </fb:login-button>
            <?php
        }
    }
}

但我从函数中收到error条消息。我无法理解为什么它会显示错误。这些方法在某天前工作得很好。还有什么我不想做到的吗?

1 个答案:

答案 0 :(得分:0)

最后我解决了这个问题。我不得不使用

$cookie = $this->get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
$_SESSION['fb_cookie'] = $cookie;

在init钩子中我检查了我的会话数据被设置为创建新用户。

function fb_login_user() {
    $cookie = $this->get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);
    if($cookie != null)
    $_SESSION['fb_cookie'] = $cookie;
    if (isset($_SESSION['fb_cookie']) && !empty($_SESSION['fb_cookie'])) {get access token and save it.