Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active August 29, 2015 14:19
Show Gist options
  • Save auycro/8f24556c4afd15c12bb4 to your computer and use it in GitHub Desktop.
Save auycro/8f24556c4afd15c12bb4 to your computer and use it in GitHub Desktop.
FacebookSDK login friends send request
<!html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
//Check Login State
function checkLoginState() {
console.log("CheckLoginState");
FB.getLoginStatus(function(response) {
FB.api('/me/friends', function(response){
if (response && response.data){
// Handle response
alert(response.data[1].name);
} else {
console.log('Something goes wrong', response);
}
});
statusChangeCallback(response);
});
}
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
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.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
//Initial FB Function
window.fbAsyncInit = function() {
//FB.Init
FB.init({
appId : 'YOUR-APP-ID',
status : true,
xfbml : true,
version : 'v2.3'
});
//FBGetLogin
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
//LoadSDK
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
//js.src = "//connect.facebook.net/en_US/sdk.js";
js.src = "//connect.facebook.net/en_US/sdk/debug.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
//TestAPI
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
});
}
function basicAPIRequest() {
console.log("CALL BASIC REQUEST");
FB.api('/me?fields=name,picture', function(response) {
//Log.info('API response', response);
document.getElementById('accountInfo').innerHTML = ('<img src="' + response.picture.data.url + '"> ' + response.name);
});
FB.api('/me/friends',function(response) {
//var friends = response.data;
if(typeof response.data[0] === 'undefined'){
console.log('NO FRIENDS');
};
//console.log(friends[1].name);
});
}
//void LogCallback(FBResult response) {
// console.log(response.Text);
//}
function testFriend() {
// fbUi("{\"message\":\"Derp\",\"filters\":[\"app_non_users\"],\"data\":\"{}\",\"app_id\":\"736362886442517\",\"locale\":\"en_US\",\"method\":\"apprequests\"}", "1");
FB.ui(
{
method: 'apprequests',
message: 'YOUR_MESSAGE_HERE',
//action_type:'send',
//object_id: '11111111111111'
//method: 'share',
//href: 'https://developers.facebook.com/docs/',
},
function(response) {
if (response && !response.error_code) {
alert('Posting completed.');
console.log(response);
} else {
alert('Error while posting.');
}
}
);
}
</script>
<div>
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
</div>
<div id="status">
</div>
<h3>Test API</h3>
<button onclick="basicAPIRequest()">Click me</button>
<div id="accountInfo" style="padding-top: 20px">accountInfo</div>
<div id="friendInfo" style="padding-top: 20px">friendInfo</div>
<button onclick="testFriend()">Click me</button>
</body>
</html>
<!html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
</head>
<body>
<script language="javascript" type="text/javascript">
var fbUser;
//Check Login State
function checkLoginState() {
console.log("CheckLoginState");
FB.getLoginStatus(function(response) {
FB.api('/me/friends', function(response){
if (response && response.data){
// Handle response
alert(response.data[1].name);
} else {
console.log('Something goes wrong', response);
}
});
statusChangeCallback(response);
});
}
// This is called with the results from from FB.getLoginStatus().
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
// Logged into your app and Facebook.
testAPI();
} else if (response.status === 'not_authorized') {
// The person is logged into Facebook, but not your app.
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.
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
//Initial FB Function
window.fbAsyncInit = function() {
//FB.Init
FB.init({
//appId : '1563078287300923',
//appId : '736362886442517',
appId : '1642943975921188',
status : true,
xfbml : true,
version : 'v2.3'
});
//FBGetLogin
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
};
//LoadSDK
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
//js.src = "//connect.facebook.net/en_US/sdk.js";
js.src = "//connect.facebook.net/en_US/sdk/debug.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
//TestAPI
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.name + '!';
fbUser = response.id;
});
}
function basicAPIRequest() {
console.log("CALL BASIC REQUEST");
FB.api('/me?fields=name,picture', function(response) {
//Log.info('API response', response);
document.getElementById('accountInfo').innerHTML = ('<img src="' + response.picture.data.url + '"> ' + response.name);
});
FB.api('/me/friends',function(response) {
//var friends = response.data;
if(typeof response.data[0] === 'undefined'){
console.log('NO FRIENDS');
};
//console.log(friends[1].name);
});
}
//void LogCallback(FBResult response) {
// console.log(response.Text);
//}
function InviteFriend() {
// fbUi("{\"message\":\"Derp\",\"filters\":[\"app_non_users\"],\"data\":\"{}\",\"app_id\":\"736362886442517\",\"locale\":\"en_US\",\"method\":\"apprequests\"}", "1");
FB.ui(
{
method: 'apprequests',
title: 'Chess Heroz Invite',
message: 'Lets play together',
filters: ['app_non_users'],
//action_type:'send',
//object_id: '11111111111111'
//method: 'share',
//href: 'https://developers.facebook.com/docs/',
},
function(response) {
console.log(response);
}
);
}
function SendGift(){
console.log("SendGift");
FB.ui({
method: 'apprequests',
title: 'Chess Heroz Gift',
message: 'Take this gift :) !',
//to: response.to,
action_type:'send',
object_id: '853625638057087'
//http://dreamatico.com/data_images/gift/gift-1.jpg
}, function(response){
console.log(response);
});
}
function PostGift() {
console.log("PostItem");
/*
FB.api(
'me/objects/chessheroz:gift',
'post',
{
//url: 'http://samples.ogp.me/826335507445254',
title: 'Sample Gift',
type: 'gift',
image: 'http://dreamatico.com/data_images/gift/gift-1.jpg',
description: 'TestGift',
app_id: '853625638057087'
},
*/
FB.api(
"/853625638057087"
,
function(response) {
// handle the response
}
)};
//AaIKTugz_5gMcD5pqzPwVF4eOpc5ctrCnPM3I4pP8ou9vzUAWOVtWiytA08j7GUWBVv_Md4MkGZku8s5DOSFD-YMdG0o1qGJAjroP0unahvxxw
//AaJmfyWvO5M6HcamHtIVu5fisdd5QySmaOxulJHomf9WAHM56FypzsdvF4xszFPr8IXanpbPSq0Wv_p94zchmL6frtElylwcLWa3IrQhLyYeHw
function InviteFriend2() {
// fbUi("{\"message\":\"Derp\",\"filters\":[\"app_non_users\"],\"data\":\"{}\",\"app_id\":\"736362886442517\",\"locale\":\"en_US\",\"method\":\"apprequests\"}", "1");
FB.ui(
{
method: 'apprequests',
//title: 'Chess Heroz Invite',
message: 'Lets play together',
//filters: ['app_non_users'],
to: "AaJmfyWvO5M6HcamHtIVu5fisdd5QySmaOxulJHomf9WAHM56FypzsdvF4xszFPr8IXanpbPSq0Wv_p94zchmL6frtElylwcLWa3IrQhLyYeHw",
//action_type:'send',
//object_id: '11111111111111'
//method: 'share',
//href: 'https://developers.facebook.com/docs/',
},
function(response) {
console.log(response);
}
);
}
/* make the API call */
function GetInviteFriend(){
FB.api(
"/me/friends?fields=id,name,picture",
function (response) {
if (response && !response.error) {
/* handle the result */
console.log(response);
FB.api("");
}
}
);
}
</script>
<div style="width:800px; margin:0 auto;">
<div>
<fb:login-button scope="public_profile,email" onlogin="checkLoginState();">
</fb:login-button>
</div>
<div id="status">
</div>
<h3>Test LoginAPI</h3>
<button onclick="basicAPIRequest()">Click me</button>
<div id="accountInfo" style="padding-top: 20px">accountInfo</div>
<!--div id="friendInfo" style="padding-top: 20px">friendInfo</div-->
<h3>Test SendRequest</h3>
<button onclick="InviteFriend2()">InviteFriend2 me</button>
<h3>Test SendItem</h3>
<button onclick="SendGift()">Click me</button>
<h3>Test PostItem</h3>
<button onclick="PostGift()">Click me</button>
<h3>Test GetInviteFriend</h3>
<button onclick="GetInviteFriend()">GetInviteFriend me</button>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment