FB 登入 API
第一步: 申請fb app
應用程式 > 設定 > 網站URL(伺服器網域 ex:http://111.11.11.2)
第二步:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://connect.facebook.net/zh_TW/all.js"></script>
<script type="text/javascript">
// 初始化 & 登入
function oplogin() {
FB.init({ appId: 'fb app id例426363157401356', status: true, cookie: true, xfbml: true }); //appid請去FB申請應用程式
fblogin();
}
function fblogin() {
FB.login(function (response) {
if (response.authResponse) {
//登入成功
FB.api('/me', function (response) {
//取得 json格式
var html = '<table>';
debugger;
for (var key in response) {
html += ('<tr>' + '<th>' + key + '</th>' + '<td>' + response[key] + '</td>' + '</tr>');
}
document.getElementById('me').innerHTML = html + '</table>';
});
}
else {
//登入失敗
alert("登入失敗。");
}
});
}
//登出
function fblogout() {
FB.getLoginStatus(function (response) {
if (response.status === 'connected') {
FB.logout(function (response) {
// user is now logged out
document.getElementById('loginform').submit();
});
} else if (response.status === 'not_authorized') {
// the user is logged in to Facebook,
// but has not authenticated your app
FB.logout(function (response) {
// user is now logged out
alert("請重新登入!");
});
} else {
// the user isn't logged in to Facebook.
alert("請重新登入!");
}
});
}
</script>
<body onLoad="oplogin()">
<form id="form1" runat="server">
<div>
<input type="button" id="btn2" onclick="javascript:oplogin();return false;" value="取得Facebook登入者資訊(PartII)" />
<div id="me"></div>
</div>
</form>
</body>
標籤: Facebook, javascript
0 個意見:
張貼留言
訂閱 張貼留言 [Atom]
<< 首頁