2016年5月9日 星期一

[FB][分享]javascript sdk

參考網址:

  1. https://dotblogs.com.tw/suehilary/2012/02/08/68441
  2. [登入] https://developers.facebook.com/docs/javascript/examples#login
  3. [javascript sdk] https://developers.facebook.com/docs/javascript/examples

javascript範例:


//判斷是否登入中
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback3(response);
});
}

// 判斷登入狀態
function statusChangeCallback3(response) {
console.log('statusChangeCallback');
// The response object is returned with a status field that lets the
// app know the current login status of the person.
// Full docs on the response object can be found in the documentation
// for FB.getLoginStatus().
if (response.status === 'connected') {
// Logged into your app and Facebook.
api_me(response);
// console.log(response);
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
console.log('not_authorized');
// document.getElementById('status').innerHTML = 'Please log ' + 'into this app.';
} else {
// The person is not logged into Facebook, so we're not sure if
// they are logged into this app or not.
console.log('else');
// document.getElementById('status').innerHTML = 'Please log ' + 'into Facebook.';
}
}

// 登入中取得資訊後,像後端傳送資料
function api_me(login_status) {
var access_token = login_status.authResponse.accessToken;
FB.ui({
method: 'feed',
name: '分享標題',
link: 'https://google.com',//分享連結
picture: 'https://google.com',//分享圖片
caption: '分享資訊',
description: '分享資訊'
}, function(response) {
if (response && response.post_id) {
var post_id = response.post_id;
FB.api('/me?fields=email,name,first_name,last_name', function(response) {
email = response.email || "";
$.ajax({
url: "", //後台api
dataType: 'json',
method: 'POST',
data: {
'post_id' : post_id,
'name' : response.name,
'account' : response.id,
'email': email,
},
success: function(result) {
console.log(result);
if (result.status=='ok') {
alert('已完成');
} else {
alert('請與客服聯絡');
}
},
error: function(err) {
console.log(err);
alert('發生錯誤');
}
});
});
} else {
alert('Post was not published.');
}
});
}

沒有留言:

張貼留言