Simple facebook API query - facebook

I am trying to do the simplest thing in the world: I want a node script to fetch the PUBLIC feed of a page.
I am trying with this:
var FB = require('fb' )
FB.api('/flourandfire/feed', 'get', function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error)
return
}
console.log(res.id)
console.log(res.name)
})
And it's not working.
{ message: 'An access token is required to request this resource.',
type: 'OAuthException',
code: 104,
fbtrace_id: 'EiH31CMsyvh' }
So, I created an "app" by the same user who owns the page and tried:
var FB = require('fb', { appId: 'XXXXXXXXXXXXXXXXXXXXX', appSecret: 'YYYYYYYYYYYYYYYYYYYYY' })
FB.api('/flourandfire/feed', 'get', function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error)
return
}
console.log(res.id)
console.log(res.name)That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and mThat is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?ake the call work?That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?
})
Still nothing.
{ message: 'An access token is required to request this resource.',
type: 'OAuthException',
code: 104,
fbtrace_id: 'EiH31CMsyvh' }
I tried adding the access token for that app:
var FB = require('fb', { appId: 'XXXXXXXXXXXXXXXXXXXXX', appSecret: 'YYYYYYYYYYYYYYYYYYYYY' })
FB.api('/flourandfire/feed', 'get', { access_token: 'ZZZZZZZZZZZZZZZZZZZZ' }, function (res) {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error)
return
}
console.log(res.id)
console.log(res.name)
})
Still nothing:
{ message: 'Invalid OAuth access token.',
type: 'OAuthException',
code: 190,
fbtrace_id: 'Hf8Gjudpwty' }
The feed is public.
So, how the HECK do I get this feed?
That is, what's the EASIEST, most permanent way to get the right access token/appId/appSecret/whatever to plumb into these functions and make the call work?

I´ve never used the fb package, but the docs tell me that this should work:
var FB = require('fb');
FB.options({appId: 'xxx', appSecret: 'xxx'});
FB.setAccessToken('APP-ID|APP-SECRET'); //btw, this is an "App Access Token". Maybe you do not even need this line if you specify the options correctly.
//FB.options({accessToken: 'APP-ID|APP-SECRET'}); //another way according to the docs
FB.api('/flourandfire/feed', (res) => {
if (!res || res.error) {
console.log(!res ? 'error occurred' : res.error);
return;
}
console.log(res);
});
Docs: https://www.npmjs.com/package/fb
More information about Tokens:
https://developers.facebook.com/docs/facebook-login/access-tokens/
http://www.devils-heaven.com/facebook-access-tokens/

Related

Ionic app with option to attend facebook events

I'm doing a hybrid app using Ionic framework.
I want to implement a facebook event system to attend some events. I have this:
ngFB.api(
'/666945880120392/attending',
'GET',
{},
function(response) {
// Insert your code here
}
);
and I have ngFB in my app.js file like this:
ngFB.init({appId: '[APP_ID]'});
The error I have is:
openfb.js:256 GET https://graph.facebook.comundefined/?access_token=undefined net::ERR_NAME_NOT_RESOLVED
I was looking for a solution but no answer. I also try in the developer facebook console and the answer is correct:
https://developers.facebook.com/tools/explorer/145634995501895/?method=GET&path=666945880120392%2Fattending&version=v2.7
What it's happening? Thanks!
EDIT 1
Thanks to #e666 I did now this
ngFB.api({
method: 'GET',
path: '/666945880120392/attending'
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
});
And the error now is that if I'm logged with an account always the return in the event is:
Object {data: Array[1], paging: Object}
And inside data is the name of another person, the unique person that is attending to the event and no the person who is logged with the access token.
Thanks!
EDIT 2
I opened another question because this is not the result than I hope, I need to go to an event, no to get the list of the people that goes to the event.
I have this now:
ngFB.api(
"/666945880120392/attending",
function (response) {
if (response && !response.error) {
console.dir(response);
}
}
);
the access_token is ok, and the response is
openfb.js:256 GET https://graph.facebook.comundefined/?access_token=EAABlK6y7pOUBAN3CPWdZB5FL…LCpXL9Bd3ELHQZAA6EJc6cCheAxUUnL59ZCZAf7aROCapJxiu991fxjDkxmMO651rfuREwZDZD net::ERR_NAME_NOT_RESOLVED
In the page of facebook https://developers.facebook.com/tools/explorer/?method=POST&path=666945880120392%2Fattending&version=v2.5
The response is
{
"error": {
"message": "(#299) Requires extended permission: rsvp_event",
"type": "OAuthException",
"code": 299,
"fbtrace_id": "G7HFJ48pbkx"
}
}
But I have this permission. Can someone help me please?
First, the path is undefined because you don't use correctly the method api. That is how you need to use it :
ngFB.api({
method: 'GET',
path: '/666945880120392/attending'
}).then(function(response) {
console.log(response);
}).catch(function(error) {
console.error(error);
});
Furthermore, you have undefined in access_token. As I looked in the library code, I am not sure that you can use this library without login as a user to have an access_token.
To have an access_token you need to use this function :
ngFB.login({scope: 'email'}).then(function(response) {
console.log('Access token' + response.authResponse.accessToken);
}).catch(function(error) {
console.error(error);
});
The access_token will then be automatically attached to request that you make with OpenFB.
You can find more complete examples of how to use the library is the github here : https://github.com/ccoenraets/OpenFB/blob/master/indexng.html

Getting all Pages user is Admin of using Graph API

I am trying to get a list of all the pages the logged user an Admin of, but I am getting the error "An active access token must be used to query information about the current user". Here is the code I am using:
function login(){
FB.login(function(response) {
if (response.authResponse) {
$.get("https://graph.facebook.com/me/accounts", function(data) {
alert( "success" );
})
.done(function() {
alert( "second success" );
})
.fail(function(data) {
alert( "error" );
})
.always(function() {
alert( "finished" );
});
} else {
// not auth / cancelled the login!
}
}, { scope: "manage_pages" });
}
So in this code, after logging in, the call $.get("https://graph.facebook.com/me/accounts") throws the error.
UPDATE:
I am able to get the the list using FB.api("/me/accounts"). So how come I can't get it using this code? What am I doing wrong? Thanks.
Simple: You are not passing a user access token with your direct request:
$.get("https://graph.facebook.com/me/accounts", function(data) { ... });
instead of
var accessToken = "aoidhfgoafhgoidfhg"; // replace with real access token
$.get("https://graph.facebook.com/me/accounts?access_token=" + accessToken, function(data) { ... });
The FB SDK does this internally/automatically. What's not really clear to me is why you don't use the FB SDK here as well, if you're already using it for FB Login. That doesn't make much sense IMHO.

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) {

cURL Error [#60, CURLE_SSL_CACERT] on Graph API post

I have a JavaScript function to post image with Graph API.
function postOpenGraphFromUrl(imageUrl, pageUrl, callback) {
var options = {
'photo' : pageUrl,
'image[0][url]' : imageUrl,
'image[0][user_generated]' : true
};
FB.api('/me/hairchalk:create', 'post', options, function(response) {
if (callback && typeof callback == 'function') {
callback(response);
}
});
}
When I try to execute I have this error in response:
/**/ FB.__globalCallbacks.f4c69a98({"error":{"type":"Exception","message":"The user generated photo at https:\/\/hairchalk.ltst.su\/uploads\/look\/file\/7\/large_photo.jpg could not be uploaded because: cURL Error [#60, CURLE_SSL_CACERT]: Transfer failed.","code":1611180}});
As I can see from the error message there is kind of problem with curl, but on my side I don't use curl at all. How can I solve this problem?
Adding the chain certification fixed this for me. Here's some instructions: http://www.entrust.net/knowledge-base/technote.cfm?tn=8166

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.