Facebook Graph API - Error on success? - facebook

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.

Related

Facebook Graph API - Adding tab

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.

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.

Publishing to the Facebook Graph API returns a strange response object

I'm using the Facebook JS SDK to publish to the GRAPH API, and while my post publishes successfully, it returns a strange response object that gets caught by the jQuery .error chain. Here's my code:
var jqxhr = $.post("https://graph.facebook.com/"+sbFacebook.authentication.userID+"/feed",
{
'message' : $("#question-text").val(),
'access_token' : sbFacebook.authentication.accessToken
}).success(
function(response) {
console.log(response);
})
.error(function(response) {
alert(response);
})
The response object looks like this:
http://img689.imageshack.us/img689/964/screenshotfrom201302222.png
Anyone have any idea what's going on and why it's getting caught by the error() function?
Thanks.
This is not how you should use Facebook JS SDK.
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);
}
});
Don't forget to initialize the JS SDK:

Publish to Current user's wall using FBJS in FBML application

I need to publish some message to the current user's wall using FBJS in FBML application.
When I use
window.fbAsyncInit = function() {
FB.init({appId: 'MY_APP_ID', status: true, cookie: true, xfbml: false});
};
i am getting an error :- FB is not defined.
and
window is not defined.
For publishing i am using this code
function graphStreamPublish(){
var body = document.getElementById("txtTextToPublish").value;
FB.api('/me/feed', 'post', { message: body }, function(response) {
if (!response || response.error) {
// alert('Error occured');
} else {
// alert('Post ID: ' + response.id);
}
});
}
(I cannot use alert in facebook..)
Thanks in advance..
If you're using FBML, you should be using FBJS instead of the Facebook Connect API.
var body = document.getElementById('txtTextToPublish').getValue();
Facebook.streamPublish(body);
Note the getValue() call instead of .value due to use of FBJS.