- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
编辑后的问题:
我正在尝试使用 this sdk 在我的 wordpress 网站上制作一个 facebook 登录插件
当我点击 facebook 按钮登录或注册时,它会通过 facebook 打开一个小窗口,上面写着继续,但之后它不会在 wordpress 数据库中创建用户并破坏 wordpress 页脚,所以它显示 An active access必须使用token查询当前用户的信息。
页面刷新后登录表单下的消息。
我完整的 facebook 登录 php 代码是这个:
<?php
include 'facebook/facebook.php';
function gazi_fb_loginForm(){
gazi_fb_error_message();
gazi_fb_LoadScript();
if(is_user_logged_in()==false){
?>
<div class="facebook_wrapper">
<img src="<?php echo plugin_dir_url(__FILE__).'images/facebook_or.png';?>" style="border:none; box-shadow:none;">
<br>
<a href="javascript:void(0)" onClick="FBLogin();">
<img src="<?php echo plugin_dir_url(__FILE__).'images/facebook_button.png';?>" alt="Fb Connect" title="Login with facebook" /></a></div>
<?php
}
}
function gazi_fb_LoadScript(){
global $wpdb;
$gazi_option=$wpdb->prefix."gazi_option";
$path = plugin_dir_url(__FILE__); // define path to link and scripts
$pageURL = get_permalink();
$sign = strpos($pageURL,'?')?'&':'?';
//facebook app secret
$qry1="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_secret'";
$facebook_app_secret = $wpdb->get_var($qry1);
$qry2="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_id'";
$facebook_app_id = $wpdb->get_var($qry2);
$facebook = new Facebook(array(
'appId' => $facebook_app_id,
'secret' => $facebook_app_secret,
));
?>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook_app_id; ?>',
xfbml : true,
status : true,
cookie : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
}
else {
console.log('initiate FB login...');
FB.login();
}
});
FB.api('/me/feed',function(response){
var idDiv=document.getElementById('result');
idDiv.textContent=JSON.stringify(response);
});
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function FBLogin(){
FB.login(function(response){
if(response.authResponse){
window.location.href = "<?php echo $pageURL.$sign;?>option=fblogin";
}
}, {scope: 'email'});
}
</script>
<?php
}
function gazi_fb_error_message(){
if(isset($_SESSION['msg'])){
echo '<div class="'.$_SESSION['msg_class'].'">'.$_SESSION['msg'].'</div>';
unset($_SESSION['msg']);
unset($_SESSION['msg_class']);
}
}
function gazi_fb_login_validate(){
$path = plugin_dir_url(__FILE__); // define path to link and scripts
$pageURL = get_permalink();
$sign = strpos($pageURL,'?')?'&':'?';
if(isset($_REQUEST['option']) && $_REQUEST['option'] == "fblogin"){
global $wpdb;
$gazi_option=$wpdb->prefix."gazi_option";
//facebook app secret
$qry1="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_secret'";
$facebook_app_secret = $wpdb->get_var($qry1);
$qry2="SELECT value FROM $gazi_option WHERE fieldname='gazi_facebook_app_id'";
$facebook_app_id = $wpdb->get_var($qry2);
$facebook = new Facebook(array(
'appId' => $facebook_app_id,
'secret' => $facebook_app_secret,
'cookie' => TRUE,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_profile = $facebook->api('/me');
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
if (!isset($user_profile['email'])) $user_profile['email'] = $user_profile['id'] . '@facebook.com';
$user_fbid = $fbuser;
$user_email = $user_profile["email"];
$user_fnmae = $user_profile["first_name"];
if( email_exists( $user_email )) { // user is a member
$user = get_user_by('login', $user_email );
$user_id = $user->ID;
wp_set_auth_cookie( $user_id, true );
} else { // this user is a guest
$random_password = wp_generate_password( 10, false );
$user_id = wp_create_user( $user_email, $random_password, $user_email );
update_user_meta($user_id, 'avtar_image', 'https://graph.facebook.com/' . $user_profile['id'] . '/picture?type=large');
wp_update_user(array(
'ID' => $user_id,
'display_name' => $user_profile['name'],
'first_name' => $user_profile['first_name'],
'last_name' => $user_profile['last_name']
));
wp_set_auth_cookie( $user_id, true );
}
wp_redirect( $pageURL.$sign.'login4=1');
exit;
}
}
}
?>
这是上面代码中的javascript。
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook_app_id; ?>',
xfbml : true,
status : true,
cookie : true,
version : 'v2.1'
});
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
console.log('Logged in.');
}
else {
console.log('initiate FB login...');
FB.login();
}
});
FB.api('/me/feed',function(response){
var idDiv=document.getElementById('result');
idDiv.textContent=JSON.stringify(response);
});
};
(function(d){
var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
}(document));
function FBLogin(){
FB.login(function(response){
if(response.authResponse){
window.location.href = "<?php echo $pageURL.$sign;?>option=fblogin";
}
}, {scope: 'email'});
}
</script>
有人知道它是如何工作的吗?我做错了什么?非常感谢任何帮助。
谢谢。
更新:
我已经使用这个答案更改了文件中的一些行 here
改变了这个: $fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_profile = $facebook->api('/me');
}
catch (Exception $e) {
echo $e->getMessage();
exit();
}
这个:
$fbuser = $facebook->getUser();
$photo_details = array('message' => 'my place');
$file='photos/my.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
if ($fbuser) {
try {
// We have a valid FB session, so we can use 'me'
$upload_photo = $facebook->api('/me/photos', 'post', $photo_details);
} catch (FacebookApiException $e) {
error_log($e);
}
我现在注册了,但是很奇怪
wordpress 的用户名和昵称总是@facebook :D
所以我认为它没有在这里获取变量:
if (!isset($user_profile['email'])) $user_profile['email'] = $user_profile['id'] . '@facebook.com'; and just keeping @facebook instead without the `$user_profile['id']`
最佳答案
请在下面找到示例代码。我使用 JavaScript SDK 进行登录,使用 PHP SDK 生成访问 token 。我希望它对你有用。
** HTML 代码::**
<html>
<body>
<p><a href="#" onClick="logInWithFacebook()">Log In with the JavaScript SDK</a></p>
<script>
logInWithFacebook = function() {
FB.login(function(response) {
if (response.authResponse) {
alert('You are logged in & cookie set!');
location.href = "//localhost/facebook/testfb.php"
// Now you can redirect the user or do an AJAX request to
// a PHP script that grabs the signed request from the cookie.
} else {
alert('User cancelled login or did not fully authorize.');
}
});
return false;
};
window.fbAsyncInit = function() {
FB.init({
appId: 'app-id',
cookie: true, // This is important, it's not enabled by default
version: 'v2.9'
});
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {
return;
}
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</body>
</html>
** PHP代码**
<?php
require_once('Facebook/autoload.php');
# /js-login.php
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.9',
]);
$helper = $fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (!isset($accessToken)) {
echo 'No cookie set or no OAuth data could be obtained from cookie.';
exit;
}
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('{app-id}'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name,email', $accessToken->getValue());
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
echo 'Email: ' . $user['email'];
?>
关于javascript - php facebook sdk 不会在 wordpress 中创建用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43632458/
我正在细读 http://www.khronos.org/网站,只找到了 OpenCL 的头文件(不是我不关心的 OpenGL)。如何获取 OpenCL SDK? 最佳答案 AMD 的 ATI Str
Android 项目中最低(最低 sdk)和最高(目标 sdk)级别是否有任何影响。这些东西是否会影响项目的可靠性和效率。 最佳答案 没有影响,如果您以 SDK 级别 8 为目标,那么您的应用将以 9
“min sdk version/target sdk version”和“compile sdk version”有什么区别?我知道 min 和 target sdk 是什么意思,但是 compil
我正在尝试运行 Dji Mobile-Sdk-Android:https://github.com/dji-sdk/Mobile-SDK-Android使用 dji 网站上的说明:https://de
我目前正在向我的 iPhone 应用程序添加新的 Facebook iOS sdk 3.1.1。我看到 sdk 有重大变化。例如。我的旧 sdk 创建一个 Facebook 对象并从 FBReques
我最近刚刚下载了 Xcode 4.6 with mac 10.8.4 with iOS 6.0 SDK package,我立即注意到我无法使用我的 iPhone 3Gs with iOS 4.6 进行
我尝试下载 OpenCL SDK。但是没办法。我有一个 AMD GPU,所以我在谷歌上搜索了 AMD SDK,但是来自谷歌的所有链接和一些教程都被破坏了,不可能通过 AMD 开发者网站找到 sdk。
安装 Google Cloud SDK 后,当我运行时 gcloud 授权登录 我收到一条错误消息: Your browser has been opened to visit: https://a
我一直在 nvidia 网站上搜索 GPU 计算 SDK,因为我正在尝试构建具有 cuda 支持的点云库 (PCL)。但是,在 nvidia 网站上,我只能找到工具包的链接,而不是 SDK 的单个下载
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。 2
当我打开 Android SDK 管理器时,会出现一个屏幕“选择要安装的包”。它列出了一堆带有绿色复选标记(已经安装)的软件包,还有一些带有 x's 的软件包,它们没有安装。如果我选择“全部接受”,它
在开发过程中,我发布了 SDK 21 Lollipop,但我无法在我的 KitKat 设备上使用它。应用程序非常简单,我只将 SDK 用于动画和 Material 设计,但是当我尝试对 Play 商店
enter image description here friend 们好 在使用 Linux、jenkins 和 docker 探索 dotnet 核心时,我遇到了构建问题,该问题在标题“/usr
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我创建了一个flutter项目,运行flutter packages get,输出如下:【二】flutter包搞定等待另一个 flutter 命令释放启动锁...第二次运行“flutter packa
这个问题在这里已经有了答案: What are the Android SDK build-tools, platform-tools and tools? And which version sh
我使用 Corona SDK 已经快一年了,并且开发了几个简单的游戏。我现在正在寻找的是在 Corona SDK 中创建 3D 幻觉的某种方法。如果有人有 Corona 3D 方面的经验,我将不胜感激
我有一个CLDC 1.1 / MIDP 2.0项目,可以使用Java ME SDK 3.2很好地进行编译。 最近,我已将Java SE升级到8,结果Java ME SDK开始在Windows启动时显示
有没有办法以编程方式控制连接到华擎主板的 RGB 照明?我知道华硕有一个 Aura SDK,但华擎是否也有一个用于他们的硬件? 如果是,我在哪里可以找到它? 最佳答案 没有用于华擎多彩的 sdk。但是
如何使用 crm 2011 sdk 和 XrmServiceContext 创建事务? 在下一个示例中,'new_brand' 是一些自定义实体。我想创建三个品牌。第三个拥有错误的 OwnerID g
我是一名优秀的程序员,十分优秀!