使用Facebook登录我的网站 [英] Using facebook login for my website
问题描述
我正在尝试将facebook登录按钮并入我网站的主页。我已经连接了一个应用程序,所以我有应用程序ID和秘密,但我不知道如何检查用户是否已经注册到我的网站,所以在我的数据库,以预览他的用户配置文件。我从Facebook使用了javaScript sdk,但是当我按下Facebook的登录按钮,授权后,没有任何反应,我的网站的主页再次显示。有谁知道如何从javascript sdk获取用户的电子邮件?我正在为我的网页使用java servlet。我使用了facebook文档中的代码。
I am trying to incorporate the facebook login button into my website's homepage. I have connected it with an app, so i have the app id and secret, but i can't figure out how to check if the user is already registered to my site, so in my database, in order to preview his userprofile. I have used the javaScript sdk from facebook but when i press the facebook's login button, after the authorization, nothing happens and my website's homepage is shown again. Does anyone know how to get the user's email from the javascript sdk? I am using java servlets for my pages. I have used the code from the facebook documentation.
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '8568345835', // App ID
channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// Additional initialization code here
};
// Load the SDK Asynchronously
(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));
</script>
<div class="fb-login-button" data-scope="email">
推荐答案
https://developers.facebook.com/docs/authentication/
客户端流
客户端流还使用用户
验证和应用程序授权的OAuth对话框。唯一的区别是你必须
必须指定一个值为令牌的 response_type
参数:
The client-side flow also uses the OAuth Dialog for user
authentication and app authorization. The only difference is that you
must specify the response_type
parameter with a value of token:
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&response_type=token
与服务器端一样流程,您还可以使用范围参数请求其他权限:
As with the server-side flow, you can also request additional permissions using the scope parameter:
https://www.facebook.com/dialog/oauth?
client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream&
response_type=token
这可能是可能的,但这不是最佳做法。我建议您介绍一些服务器端脚本(即PHP),但是如果您仅使用Facebook Javascript SDK来获取某些用户数据,则该方法可能会有用。工作流将是这样的。
It might be possible but it's not a best practice. I would suggest introduction of some server side scripting (i.e. PHP), but if you are using Facebook Javascript SDK only to obtain some user data than this method can be useful. Workflow would be like this.
-
用户点击重定向到的Facebook Connect按钮:
User clicks Facebook Connect button that redirects to:
https://www.facebook.com/dialog/oauth 的?
client_id = YOUR_APP_ID& redirect_uri = YOUR_URL& response_type = token
https://www.facebook.com/dialog/oauth? client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&response_type=token
Facebook检查YOUR_APP_ID和URL并执行身份验证
Facebook checks YOUR_APP_ID and URL and performs authentication
重定向到您的网址。
http://YOUR_URL#access_token=166942940015970%7C2.sa0&expires_in=64090
...在URL中有访问令牌。
...with access token in URL.
稍后,您将从URL并使用它通过AJAX请求执行FQL(Facebook查询语言),并获取用户数据。
Later you would extract token from URL and use it to execute FQL(Facebook Query Language) via AJAX request, and obtain user data.
此处描述: https:// developers.facebook.com/docs/reference/fql/
这篇关于使用Facebook登录我的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!