Facebook Graph API - Adding tab - facebook

I am trying to add a tab trought the facebook js api, and it fails:
Login with permissions:
FB.login(function(response) {
console.log(response);
}, {
scope: 'publish_actions, manage_pages'
});
FB.login(function(response) {console.log(response);
}, {perms:'read_stream,publish_stream,offline_access'});
reading pages to get token:
FB.api(
"/me/accounts",
function (response) {
if (response && !response.error) {
console.log(response)
}
}
);
adding a tab, for example, these two methods:
a.
FB.api(
"/{page_id}/tabs/",
"POST",
{
"app_id": "{app_id}",
access_token: "{token}"
},
function (response) {
console.log(response)
}
);
I get response:
Object {success: true}
b. tried directly:
jimdoGen002.getJSON('https://graph.facebook.com/{page_id}/tabs?app_id={app_id}&access_token=token&callback=?', function(r){console.log(r)})
I get response, array with the tabs the user page has, but without my new tab.
going to the user page - no new tab.
My questions:
what am I missing? why do I get sucess but cant get the tab?
if its because the review of "manage_pages" by facebook is not complete yet - why do I get success?
if the above - how can I test manage pages BEFORE I post it to facebook review?
Can I add a tab WITHOUT an app inside? just an iframe url?
Thanks in advance,
Lior.
(to make it all simple:)
FB.api(
"/me/accounts",
function (response) {
var token = response.data[0].access_token;
var id = response.data[0].id;
console.log(response.data[0].name);
FB.api(
id + '/tabs',
"POST",
{
"app_id": {app_id},
"access_token": token
},
function (response) {
console.log(response);
FB.api(
id + '/tabs/{app_id}',
function (response) {
console.log(response)
},
{
access_token: token
},
function(response) {
console.log(response)
}
);
}
);
}
);
result:
my page
Object {success: true}
Object {data: Array[2]} - none is my app.

Related

Facebook graph api adding custom tab not working

I'm trying to add custom tab using facebook graph api. I tried everything on the doc but nothing is working. Here's my sample code.
$('#addTab').click(function(event){
FB.ui({
method: 'pagetab',
redirect_uri: 'https://{mydomain}.com',
}, function(response) {
if((response && !response.error_code) || window.mobileAndTabletCheck()) {
console.log(response);
FB.api(
'/{mypageid}/tabs',
'POST',
{"custom_name":"My Custom Tab","tab":"app_{myappid}",
"access_token":"{I put my access token here}"},
function(response) {
// Insert your code here
//
}
);
} else {
console.log('error');
}
});
});
But I'm getting this messages from the two method
FB.ui
FB.api

Facebook Post through Graph API with access token

I am developing a facebook app where I need impersonation of users, so there is an option when you use facebook for "Use Facebook As", i need to implement the same in my app.
I see the access tokens retrieved for the pages I own, but I don't understand how to use this with the following graph api to post
var body = 'Reading JS SDK documentation';
FB.api('/me/feed', 'post', { message: body }, function (response) {
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
so my question is how do I set the access token for the api in this case??
I Hope, am clear with my question
Add it with the parameters. Change this line
FB.api('/me/feed', 'post', { message: body }, function (response) {
to...
var my_token = "the access token value";
FB.api('/me/feed', 'post', { message: body, access_token : my_token }, function (response) {

Post a public message in Facebook with FB.api

I have tried to post a message on my wall using FB.api(). I posted it successfully but it's showing only for me. I want to make this post public.
In app Configuring permissions i set "Default Activity Privacy" value Public but still message posting private.
I tried to do it by following code:
FB.login(function (response) {
if (response.authResponse) {
var privacy = { 'value': 'EVERYONE' };
var txt = 'my post to test feed post using api';
FB.api('me/feed', 'post', { message: txt, privacy: privacy }, function (response) {
if (!response || response.error) {
alert(JSON.stringify(response.error));
} else {
alert('Post ID: ' + response.id);
}
});
}
}, { scope: 'email,user_likes,publish_actions,publish_stream,read_stream' });
Please check my code.
As said in reference 'privacy' field value should be string.
Try to pass "{ 'value': 'EVERYONE' }" as string, not an object.

Facebook Graph API - Error on success?

I am using the Facebook Javascript SDK to upload a photo to the user's timeline. This is my call:
function post_photo_to_facebook() {
var file_name = 'https://my-image.url.com/image.jpg';
var access_token = FB.getAuthResponse()['accessToken'];
$.ajax({
type: "POST",
url: "https://graph.facebook.com/me/photos",
data: {
message: "Here is my message",
url: file_name,
access_token: access_token,
format: "json"
},
success: function(data){
alert("POST SUCCESSFUL");
},
error: function(data){
alert('Error');
console.log(data);
}
});
}
When in Chrome, I am receiving an Error back from this AJAX call, yet the statusText is "OK", and the image is being successfully uploaded to my timeline. I am just wondering what I am missing here - why is the error being called?
You should be using FB.api to upload images, rather than ajax POST due to CORS reasons.
So your code above would look like this:
var file_name = 'https://my-image.url.com/image.jpg';
FB.api('/me/photos', 'post', {
message:'Here is my message',
url:file_name
}, function(response){
if (!response || response.error) {
alert('Error occured');
} else {
alert('Post ID: ' + response.id);
}
});
and you use FB.init to set up your tokens etc.

Check if Facebook JS-API app already has been given permission

I have a small script that unlocks some content on a page when a user logs in via Facebook and posts some information on their wall.
I want to avoid any duplicate posts (spam) in the event the user visits the page a second time and repeats the process.
Is there a way of (when asking for the permissions) to tell if they have already been given and therefore skip the sharing part of the script?
Here's my code:
$('.preview a').click(function(e) {
e.preventDefault();
FB.login(function(response) {
if (response.session) {
FB.api('/me/feed', 'post', {
message: 'Just unlocked the exclusive preview of...',
picture: 'http://website.com/share.png',
link: 'http://www.website.com',
name: 'Name goes here',
description: 'Description here'
},
function(response) {
if (!response || response.error) {
alert(response.error);
} else {
$('.preview').hide();
$('.clip').show();
}
});
}
},
{perms:'read_stream,publish_stream'});
});
From 2011 to 2014 a lot of things change..
I made this solution to check for "user_friends" and "publish_actions" permissions, if not allowed the both, force the user to "re-autenticate". The callback method will only be called when all permissions are given.
function login(cb,forceAuth) {
FB.getLoginStatus(function (response) {
if (response.status !== 'connected' || forceAuth==true){
FB.login(function (response) {
checkPermissions(cb,false);
}, {scope: 'publish_actions,user_friends'});
} else {
checkPermissions(cb);
}
});
}
function checkPermissions(cb,forceAuth){
FB.api(
"/me/permissions",
function (response) {
if (response.data[0]['publish_actions']==undefined || response.data[0]['publish_actions']==0 ||
response.data[0]['user_friends']==undefined || response.data[0]['user_friends']==0) {
if (forceAuth!=false)
login(cb,true);
} else {
cb();
}
}
);
}
How to use:
login(function () {
myLogedAndAllowedFunction();
});
I've written a tutorial about this here, and here's an example:
FB.api({ method: 'fql.query', query: 'SELECT read_stream,offline_access,publish_stream FROM permissions WHERE uid=me()' }, function(resp) {
for(var key in resp[0]) {
if(resp[0][key] === "1")
console.log(key+' is granted')
else
console.log(key+' is not granted')
}
});
There's also a JS REST example there in the article just for reference.