我使用以下代码允许访问者通过Facebook加入我的博客。用户可以成功登录,但问题是WordPress没有存储电子邮件ID。可能是一个愚蠢的错误,但我没有得到它。
function.php
// Facebook Login Button
require_once("inc/facebookoauth.php");
class Facebook_Login_Widget extends WP_Widget
{
public function __construct()
{
parent::__construct("facebook_login_widget", "Facebook Login", array("description" => __("Display a Facebook Login Button")));
}
public function form( $instance )
{
// Check values
if($instance)
{
$title = esc_attr($instance['title']);
$app_key = $instance['app_key'];
$app_secret = $instance['app_secret'];
}
else
{
$title = '';
$app_key = '';
$app_secret = '';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'facebook_login_widget'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('app_key'); ?>"><?php _e('App ID:', 'facebook_login_widget'); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('app_key'); ?>" name="<?php echo $this->get_field_name('app_key'); ?>" value="<?php echo $app_key; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('app_secret'); ?>"><?php _e('App Secret:', 'facebook_login_widget'); ?></label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('app_secret'); ?>" name="<?php echo $this->get_field_name('app_secret'); ?>" value="<?php echo $app_secret; ?>" />
</p>
<?php
}
public function update( $new_instance, $old_instance )
{
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['app_key'] = strip_tags($new_instance['app_key']);
$instance['app_secret'] = strip_tags($new_instance['app_secret']);
update_option("facebook_app_id", $new_instance['app_key']);
update_option("facebook_app_secret", $new_instance['app_secret']);
return $instance;
}
public function widget( $args, $instance )
{
extract($args);
$title = apply_filters('widget_title', $instance['title']);
echo $before_widget;
if($title)
{
echo $before_title . $title . $after_title ;
}
if(is_user_logged_in())
{
?>
<a href="<?php echo wp_logout_url( get_permalink() ); ?>" title="Logout"><input type="button" value="Logout" /></a>
<?php
}
else
{
?>
<a href="<?php echo site_url() . '/wp-admin/admin-ajax.php?action=facebook_oauth_redirect'; ?>"><input type="button" value="Login Using Facebook" /></a>
<?php
}
echo $after_widget;
}
}
register_widget("Facebook_Login_Widget");
INC / facebookauth.php
<?php
session_start();
function facebook_oauth_redirect()
{
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require_once("../wp-load.php");
//construct URL and redirect
$app_id = get_option("facebook_app_id");
$redirect_url = get_site_url() . "/wp-admin/admin-ajax.php?action=facebook_oauth_callback";
$permission = "email,name";
$final_url = "https://www.facebook.com/dialog/oauth?client_id=" . urlencode($app_id) . "&redirect_uri=" . urlencode($redirect_url) . "&permission=" . $permission;
header("Location: " . $final_url);
die();
}
add_action("wp_ajax_facebook_oauth_redirect", "facebook_oauth_redirect");
add_action("wp_ajax_nopriv_facebook_oauth_redirect", "facebook_oauth_redirect");
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function facebook_oauth_callback()
{
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require_once("../wp-load.php");
if(isset($_GET["code"]))
{
$token_and_expire = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=" . get_option("facebook_app_id") . "&redirect_uri=". get_site_url() . "/wp-admin/admin-ajax.php?action=facebook_oauth_callback" . "&client_secret=" . get_option("facebook_app_secret") . "&code=" . $_GET["code"]);
parse_str($token_and_expire, $_token_and_expire_array);
if(isset($_token_and_expire_array["access_token"]))
{
$access_token = $_token_and_expire_array["access_token"];
$user_information = file_get_contents("https://graph.facebook.com/me?access_token=" . $access_token . "&fields=email,name");
$user_information_array = json_decode($user_information, true);
$email = $user_information_array["email"];
$name = $user_information_array["name"];
if(username_exists($name))
{
$user_id = username_exists($name);
wp_set_auth_cookie($user_id);
update_user_meta($user_id, "facebook_access_token", $access_token);
header('Location: ' . get_site_url());
}
else
{
//create a new account and then login
wp_create_user($name, generateRandomString(), $email);
$user_id = username_exists($name);
wp_set_auth_cookie($user_id);
update_user_meta($user_id, "facebook_access_token", $access_token);
header('Location: ' . get_site_url());
}
}
else
{
header("Location: " . get_site_url());
}
}
else
{
header("Location: " . get_site_url());
}
die();
}
add_action("wp_ajax_facebook_oauth_callback", "facebook_oauth_callback");
add_action("wp_ajax_nopriv_facebook_oauth_callback", "facebook_oauth_callback");
我尝试在update_user_meta($user_id, "facebook_access_token", $access_token);
中添加$ email,但它也无效。
答案 0 :(得分:0)
更改此
$final_url = "https://www.facebook.com/dialog/oauth?client_id=" . urlencode($app_id) . "&redirect_uri=" . urlencode($redirect_url) . "&permission=" . $permission;
要强>
$final_url = "https://www.facebook.com/dialog/oauth?client_id=" . urlencode($app_id) . "&redirect_uri=" . urlencode($redirect_url) . "&scope=" . $permission;
CBroe是对的,
您必须使用&scope=
来请求额外的权限。
我希望这会对你有所帮助