我正在使用Facebook应用程序是否有任何方法通过使用Facebook登录按钮获取用户身份验证令牌我尝试了许多来自谷歌的例子但没有获得成功。请为我提供所有必需代码的解决方案。基本上我想让用户填写facebook的uname和pwd 并获取auth令牌以保存在db中,我将来使用它来获取用户信息。
这是示例,但不起作用
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://connect.facebook.net/en_US/all.js" type="text/javascript" charset="utf-8"></script>
<body>
<p>
<input type="email" id="email" placeholder="email" />
</p>
<p>
<input type="password" id="password" placeholder="password" />
</p>
<p>
<input type="button" onclick="" value="Authorize in Facebook using hidden iframe!"
id="iLogin" />
</p>
<iframe id="fbFrame" src="about:blank;" border="0"></iframe>
<p style="color: green; font-size: 30px;" id="fbtoken">
</p>
<p style="color: green; font-size: 30px;" id="fbuid">
</p>
<script>
var my_client_id = "my_client_id",
my_redirect_uri = "http://www.facebook.com/connect/login_success.html",
my_type = "user_agent",
my_display = "popup"
authorize_url = "https://graph.facebook.com/oauth/authorize?"
authorize_url += "client_id=" + my_client_id
authorize_url += "&redirect_uri=" + my_redirect_uri,
authorize_url += "&type=" + my_type,
authorize_url += "&display=" + my_display,
authorize_url += "&scope=offline_access"
setIFrameUrl = function () {
$("#fbFrame").attr("src", authorize_url);
}
getToken = function () {
debugger;
var loc = document.getElementById("fbFrame").contentWindow.location.href
if (/access_token/.test(loc)) {
var fbToken = loc.match(/access_token=(.*)$/)[1]
var fbId = loc.match(/-(\d*)%/)[1]
$("#fbtoken").html("Facebook access token: " + fbToken);
$("#fbuid").html("Facebook user id: " + fbId);
} else {
var grantAccessButton = $("#fbFrame").contents().find("input[name=grant_clicked]");
if (grantAccessButton) { // if app asks for permissions, grant 'em
grantAccessButton.click();
}
setTimeout(getToken, 1000)
}
}
doTheLogin = function () {
var emailInput = $("#fbFrame").contents().find("input[name=email]");
var passInput = $("#fbFrame").contents().find("input[name=pass]");
var loginButton = $("#fbFrame").contents().find("input[name=login]");
emailInput.val($("#email").val());
passInput.val($("#password").val());
loginButton.click();
}
$(document).ready(function () {
setIFrameUrl();
$("#iLogin").click(function () {
console.log("iLogin clicked!")
debugger;
doTheLogin();
})
getToken();
})
</script>
现在我使用以下代码
Facebook login JS - FB.Event.subscribe('auth.login') triggers without login button click
但仍然无法获取accessToken有一个名为Facebook_login()的函数将获得Tocken但它从未调用过我需要做的任何其他更改或我做错了实现。我用代码作为
<script>
window.fbAsyncInit = function () {
FB.init({
appId: 'appId',
status: false,
cookie: true,
xfbml: true
});
};
(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 Facebook_login() {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
var uid = response.authResponse.userID;
var accessToken = response.authResponse.accessToken;
alert(accessToken);
}
});
}
</script>
<div class="fb-login-button" onlogin="Facebook_login()" autologoutlink="true" scope="publish_stream,offline_access,manage_pages,user_groups">
</div>