Getting all Pages user is Admin of using Graph API - facebook

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.

Related

React Native Facebook SDK ShareDialog asks to login again

I'm currently doing a project using react native where I need to let users to login using facebook. So I use React-native-fbsdk LoginButton component to let users to login and get the access token. Also part of the project needs let users to share links to their profiles.
In order to do that, I followed as GitHub repo of Facebook which says how to add ShareDialog. After adding both LoginButton and ShareDialog, I ran the app. Login was successful and I got the access token. But when I try to share a link, it asks me to login again. If I login with ShareDialog, it let me to share post and everything as expected.
But if I logout with LoginButton after login with ShareDialog too, both components perform logout successfully. Problem is Login does not handle for both components with single login.
Below is my code.
render() {
return (
<View style={styles.container}>
<LoginButton
publishPermissions={["publish_actions"]}
onLoginFinished={
(error, result) => {
if (error) {
alert("Login failed with error: " + error.message);
} else if (result.isCancelled) {
alert("Login was cancelled");
} else {
alert("Login was successful with permissions: " + result.grantedPermissions)
}
}
}
onLogoutFinished={() => alert("User logged out")}/>
<TouchableHighlight onPress={this.shareLinkWithShareDialog}>
<Text style={styles.shareText}>Share link with ShareDialog</Text>
</TouchableHighlight>
</View>
);
}
shareLinkWithShareDialog = () => {
const shareLinkContent = {
contentType: 'link',
contentUrl: 'https://www.sample.com/',
contentDescription: 'Test Sharing Description'
};
var tmp = this;
ShareDialog.canShow(shareLinkContent).then(
(canShow) => {
if (canShow) {
return ShareDialog.show(shareLinkContent);
}
}
).then(
(result) => {
if (result.isCancelled) {
alert('Share cancelled');
} else {
alert('Share success with postId: ' + result.postId);
}
},
(error) => {
alert('Share fail with error: ' + error);
}
);
}
Please help me here. I cannot identify what's wrong with this.
Note
I checked this using a Test User of Facebook and it also has all relevant permissions including manage_pages and test user has it's own Test Page too.
I got a similar problem and that was because I hadn't installed facebook app on my phone. Seems like your code is fine.
However I haven't used Test Page and Test User options in facebook but I hope they are no different than a normal user when it comes to functionalities. Just try to install the facebook app and run your app. Should be fine.

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

How to get email from facebook in cordova application?

i want to get email after login with my cordova application
but when i added a permission with Graph API Explorer , i found this
message
yes i added permission :
in my controller :
.controller("LoginController", function($scope, $cordovaOauth, $localStorage, $location) {
$scope.login = function() {
$cordovaOauth.facebook("app-id", ["email", "user_birthday","user_website", "user_location", "user_relationships"]).then(function(result) {
$localStorage.accessToken = result.access_token;});
.controller("ProfileController", function($scope, $http, $localStorage, $location) {
$scope.init = function() {
if($localStorage.hasOwnProperty("accessToken") === true) {
$http.get("https://graph.facebook.com/v2.2/me", { params: { access_token: $localStorage.accessToken, fields: "id,email,name,gender,email,location,website,picture,relationship_status", format: "json" }}).then(function(result) {
$scope.profileData = result.data;
//alert(JSON.stringify(result.data));
}, function(error) {
//alert("There was a problem getting your profile. Check the logs for details.");
console.log(error);
});
i found this result :
[![enter image description here][4]][4]
Users don't need an email to use Facebook afaik, and it may not be confirmed yet. The debug message is very clear about that. There is no guarantee to always get the Email, even with the email permission.
Just to make sure, test this API call: https://developers.facebook.com/tools/explorer/?method=GET&path=me%3Ffields%3Demail&version=v2.5

Cordova: Can't able to get user information, it shows 'malformed access token'

I am about to finish my cordova application where i am using the facebook social login. I am trying to access the user information from the graph API that openfb.js is calling. My code snippet is this:
function login() {
openFB.login(
function (response) {
if (response.status === 'connected') {
alert(JSON.stringify(response));
FACEBOOK_TOKEN = response.authResponse.accessToken;
var message = "Connected to Facebook_____";
alert('Message is :' + message);
alert('Access token is :' + FACEBOOK_TOKEN);
getInfo(response.authResponse.accessToken);
} else {
var errorMessage = 'Facebook login failed: ' + response.error + "_____";
alert(errorMessage);
}
}, {scope: 'email,public_profile,user_birthday,user_location'});
}
function getInfo(access_token) {
openFB.api({
path: '/2.5/me?access_token=' + access_token,
success: function (data) {
alert(JSON.stringify(data));
var message = "Logged in with Facebook as " + data.name + "_____";
alert(message);
},
error: errorHandler
});
}
function errorHandler(error) {
var errorMessage = error.message;
alert('Error is : '+errorMessage);
}
Here in this case when i am getting the access token of the user but when i pass it to graph API that is in the get info function, then it shows me the error:
Error is : Malformed access token CAANoTLg2W..........
I am not able to debug the error ocuring here. Please help me resolve it.
Thank in advance for any response.

Getting user app token using react-native facebook SDK

I've gotten facebook login working using the new https://github.com/facebook/react-native-fbsdk, however I can't seem to get the user token which is usually returned in the response. Anyone found a way to get this information? The other SDK's https://github.com/magus/react-native-facebook-login returns it on the login request but the new FB sdk doesn't, and the documentation on the github page doesn't mention it anywhere.
Found the answer after some more digging. You need to use the fbsdkcore to access the user token. Heres how you use it.
var FBSDKCore = require('react-native-fbsdkcore');
var {
FBSDKAccessToken,
} = FBSDKCore;
var Login = React.createClass({
render: function() {
return (
<View>
<FBSDKLoginButton
onLoginFinished={(error, result) => {
if (error) {
alert('Error logging in.');
} else {
if (result.isCanceled) {
alert('Login cancelled.');
} else {
FBSDKAccessToken.getCurrentAccessToken((token) => {
console.log(token.tokenString);
})
}
}
}}
onLogoutFinished={() => console.log('Logged out.')}
readPermissions={[]}
publishPermissions={['publish_actions']}/>
</View>
);
}
});
You don't need a third party module (react-native-fbsdkcore)
The package (react-native-fbsdk) has instructions on it's Github page: https://github.com/facebook/react-native-fbsdk#usage
After you've logged the user in, you can fetch the access credentials as follows:
import { AccessToken } from 'react-native-fbsdk';
AccessToken.getCurrentAccessToken().then(data => {
console.log(data.accessToken.toString())
})