我找到了一个官方(但未维护,现已正式放弃)示例客户端 JavaScript 库实现示例的存储库,单击此处查看最新的 fork。这是该页面中的 JQuery 示例:
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Connect JavaScript - jQuery Login Example</title>
</head>
<body>
<h1>Connect JavaScript - jQuery Login Example</h1>
<div>
<button id="login">Login</button>
<button id="logout">Logout</button>
<button id="disconnect">Disconnect</button>
</div>
<div id="user-info" style="display: none;"></div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
</script>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
// initialize the library with the API key
FB.init({
apiKey: '48f06bc570aaf9ed454699ec4fe416df'
});
// fetch the status on load
FB.getLoginStatus(handleSessionResponse);
$('#login').bind('click', function () {
FB.login(handleSessionResponse);
});
$('#logout').bind('click', function () {
FB.logout(handleSessionResponse);
});
$('#disconnect').bind('click', function () {
FB.api({
method: 'Auth.revokeAuthorization'
}, function (response) {
clearDisplay();
});
});
// no user, clear display
function clearDisplay() {
$('#user-info').hide('fast');
}
// handle a session response from any of the auth related calls
function handleSessionResponse(response) {
// if we dont have a session, just hide the user info
if (!response.session) {
clearDisplay();
return;
}
// if we have a session, query for the user's profile picture and name
FB.api({
method: 'fql.query',
query: 'SELECT name, pic FROM profile WHERE id=' + FB.getSession().uid
},
function (response) {
var user = response[0];
$('#user-info').html('<img src="' + user.pic + '">' + user.name).show('fast');
});
}
</script>
随时提出改进建议。理想情况下,我希望看到:
- 原生 JavaScript 实现
- JSONRPC 调用以方便在个人服务器端登录
- 路由(即:成功登录后重定向到登录用户主页)