I'm using Facebook's javascript SDK to inspect the friends list of users registered in my application.
I have this call:
FB.getLoginStatus(function (response) {
if (response.status == 'connected') {
FB.api('me/friends', { fields: 'id, first_name, picture', limit: 6 },function(response){
MY CODE TO VIEW FRIENDS PICTURE
});
}
else{
CODE TO GET ACCESS_TOKEN
}
});
but in most of cases I get an api error like this:
response.error = {
message: unkown error,
code: undefined,
type: http,
subcode: undefined
}
I tested it with my account and it works fine, even if I change privacy permission as friend's sharing.
There is something wrong in my code?
FB.api('me/friends', { fields: 'id, first_name,picture', limit: 6 },function(response){
console.log(response);
});
For more detail information use Facebook's graph API explore :Facebook Graph aPI
Why don't you use an fql query with FB.api? It offers a great control.
The following FQL return (User ID, First Name and Picture of a friend who is using the application)
FB.api(
{
method: 'fql.query',
query: 'SELECT uid,first_name,pic FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) and is_app_user limit 6',
access_token: token
}
Note: You would better use this condition
if (response && response.authResponse) {
Instead of:
if (response.status == 'connected') {
Regards,
Related
I gone through this question, but as everyone says I'm not getting even the registered app users for frindslist. I always get data[] empty array. The app is in development mode, I have one admin and one tester added in https://developers.facebook.com/apps/{appid}/roles/. Both users logged in to my app. Still GraphAPI returns only empty data[]. What I'm doing wrong? I'm using react native fbsdk. The following is the code I'm using and getting "Success fetching data: {"data":[]}"
const { accessToken } = await AccessToken.getCurrentAccessToken();
const facebookCredential = firebaseAuth.FacebookAuthProvider
.credential(accessToken.toString());
const user = await firebaseAuth().signInWithCredential(facebookCredential);
const postRequestConfig = {
httpMethod: 'GET',
version: 'v2.7',
accessToken: accessToken.toString()
};
const infoRequest = new GraphRequest(
`/${user.providerData[0].uid}/friendlists`,
postRequestConfig,
(error, result) => {
if (error) {
alert('Error fetching data: ' + JSON.stringify(error));
} else {
alert('Success fetching data: ' + JSON.stringify(result));
}
},
);
new GraphRequestManager().addRequest(infoRequest).start();
The API would be /me/friends, not /me/friendlists - and there is no need to use the User ID, just use /me.
/me/friendlists would give you a list of your friendlists, NOT the friends. Make sure you understand the difference.
Also, of course both users must authorize the app with the user_friends permission.
I am using an old SDK connection that works fine
<script src="http://connect.facebook.net/fr_FR/all.js"></script>
<fb:login-button show-faces="false" scope="email,user_birthday,user_location" size="medium">Facebook
</fb:login-button>
FB.api('/me?',
{ fields: 'name, email, gender,first_name,last_name,birthday,location' },
function(response) {
console.log(response);
});
that works fine.
When I try to add fields such as: user_location, user_birthday
response gives: error
Please see
https://developers.facebook.com/docs/graph-api/reference/user#Reading
for an overview which fields are available with the User object. The fields you're apparantly interested in are location and birthday.
FB.api('/me',
{ fields: 'name, email,gender,first_name,last_name,location,birthday' },
function(response) {
console.log(response);
}
);
I am trying to delete an application tab from a facebook page.
According to the documentation, I should issue a DELETE request to "https://graph.facebook.com/PAGE_ID/tabs/TAB_ID" with a PAGE access token, but when I do so I get the error "(#210) Subject must be a page."
The same happens when trying to update a tab.
I have requested the user for "manage_pages" permission and I have the correct access_token (Adding a tab works perfectly).
the exact request is: https://graph.facebook.com/212757048770606/tabs/app_289329597785433 (with an access token)
Does anyone know what am I doing wrong?? or is there an open bug report?
Thanks alot
I don't have a solution for you, but I do know that I had some problems with removing a tab that boiled down to the fact that the tab's ID (returned from a call to get /PAGE_ID/tabs) already includes the page ID and "tabs" path.
Initially I was building my URL by taking the tab ID and sticking it on the end of /PAGE_ID/tabs/, but that didn't work because the URL ended up being something like /12345/tabs/12345/tabs/app_4567. Once I realized that the tab ID was sort of "compound" already, I got the Remove to work.
Add the page access token to the call of Facebook API
var PageAccessToken = 123456789123456789123456789123456789;
FB.api(
"/{page_id}/tabs",
"POST",
{
"object": {
"app_id": "{page_id}"
}
},{
"access_token": PageAccessToken
},
function (response) {
if (response && !response.error) {
console.log(response);
} else {
console.log(response.error);
}
}
);
function DeleteTabPage(){
var pid = page_id;
var at = access_tocken;
debugger;
FB.api(pid + '/tabs/app_{your app id}', 'DELETE', { app_id: your app id, access_token: at }, function (response) {
debugger;
if (!response || response.error) {
debugger;`enter code here`
alert('Facebook add app error ' + response.error);
} else {
console.log(response);
debugger;
// alert('App has been added');
}
}); /* end of page/tabs*/
}
I am working on a Facebook canvas iFrame application, and I`m going insane.
I am trying to check if a user is a fan of the page where the app is located, so that I can allow or disallow voting.
I use the following code:
function CheckFan() {
FB.init({
appId: 'xxxxxxxxxxxxxxxxxxxxxxxxxx',
status: true, // check login status
cookie: true, // enable cookies to allow the server to access the session
xfbml: true // parse XFBML
});
FB.api({ method: 'pages.isFan', page_id: '145116742177104' }
, function(resp) {
if (resp) { $('#main_frame').show(); $('#non_fan').hide(); }
else { $('#main_frame').hide(); $('#non_fan').show(); }
});
}
This JS SDK is driving me up the wall, while calling the documentation "incomplete" is an insult to incompleteness.
Any input will be appriciated.
Thank you!
-Elad
This has been deprecated by Facebook.
A new Graph API alternative will hopfuly be available by the time we need to deploy the app.
For now I use FQL:
FB.api({ method: 'fql.query', query: 'SELECT uid FROM page_fan WHERE uid= ' + user_id + ' AND page_id=145116742177104' },
function(result) {
if (result.length)
{ $('.main_frame').show(); $('#non_fan').hide(); } else { $('.main_frame').hide(); $('#non_fan').show(); }
});
i have:
user facebook data (
https://graph.facebook.com/btaylor )
facebook application data (
https://graph.facebook.com/2439131959
)
Is somehow possible to get if user is application administrator in facebook with this in my hands?
To get this kind of information you can use FQL:
protected function authorizeByFb(){
$result = $this->fb->api(array(
'method' => 'fql.query',
'query' => "SELECT developer_id FROM developer WHERE application_id='{$appId}' AND developer_id='{$fbUserId}'",
));
if(count($result)==1){
return true;
}
return false;
}
if your developed application is checking the role of the logged in facebook user or simply your application wants to recognize it's owner or admin,
then
http://developers.facebook.com/tools/explorer/?method=GET&path=me%2Faccounts should have that application id listed
api call:
FB.api('/me/accounts', 'get', {"access_token":access_token}, function(response){
//loop through response to check any application id matches current application id
});
response sample:
{
"name": "app_name",
"access_token": "current_token_here",
"category": "Application",
"id": "your_owned_app_id"
},
{
"name": "app_name1",
"access_token": "current_token_here",
"category": "Application",
"id": "your_owned_app1_id"
},
I just got this to work using the Facebook JS API:
FB.api({ method: 'pages.isAdmin', page_id : fbAppId }, function(response){
if(response){
alert('the user is an admin');
}else{
alert('the user is not an admin')
}
});
It's using the FB.api method to access the old REST API. This assumes that you've already called FB.init, so make sure that this comes after your init code.
Cheers,
Jeremy
You can get the signed_request and then check if page_admin = 1
<?php
$signed_request = $facebook->getSignedRequest();
$page_admin = $signed_request["page"]["admin"];
if ( $page_admin == 1 ){
echo 'Welcome Admin!';
}
?>
I tried to attempted javascript form above and found this method to work as an alternative now that the old restful api is depreciated.
function checkAdmin(fbUID, fbAppID){
FB.api({
method: 'fql.query',
query: 'SELECT role FROM app_role WHERE developer_id ='+fbUID+' AND application_id = '+fbAppID
},
function(response) {
if(response.length){
alert('User is an Admin');
}
else{
alert('User is not an Admin')
}
});
}