I'm testing how all the facebook api calls work, and I need to know how it is possible to print user_friends on a page, after the user gives permission.
I wrote this code, in order to try and print the user_friends feilds:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me', function(response) {
console.log('Successful login for: ' + response.name);
document.getElementById('status').innerHTML =
'Thanks for logging in, ' + response.user_friends + '!';
});
}
At the end, I added response.user_friends, however I don't get the user friends list printed on the page, instead I get "undefined".
If I change it to response.name, it work and the name of the user is printed.
P.S.
In the log in button I requested the user friends list:
<fb:login-button scope="public_profile,email,user_friends" onlogin="checkLoginState();">
</fb:login-button>
It is just a test for me to understand how the user_friends look like, I'm not going to do so in the actual app.
So Why I'm not getting the user_friends printed at the page?
Thanks
You have to authorize the user with the correct permission (user_friends) before making any API call. See those links for more information about user authorization with the JavaScript SDK:
https://developers.facebook.com/docs/facebook-login/login-flow-for-web/v2.2
http://www.devils-heaven.com/facebook-javascript-sdk-login/
After that, you can use /me/friends to get the friends of the authorized user. Keep in mind that you can only get the friends who authorized the App too, for privacy reasons. More information about that can be found in the following thread: Facebook Graph Api v2.0+ - /me/friends returns empty, or only friends who also use my app
Related
I am stuck with access rights in trying to post on facebook company page.
I have created an application and gotten appId and secret.
I have linked the application to the existing facebook page.
I have retrieved an accessToken for the appId.
But get the response: "(#200) The user hasn't authorized the application to perform this action."
Which user does the error statement refer to? the AppId user? The administrators of the page (me)? And where can I grant these missing rights?
I am trying to achieve that the post functionality is not facebook-user-dependent, but is company (or appID) dependent.
I am really confused about this...
Off course we could create a bogus user (kept in the company files) and post as this user - but that goes against the Facebook policy, and that is not the road we want to go down...
function FB_doPost(link, message, accessToken) {
console.info('doPost');
FB.api('/pageid/feed', 'post', {
access_token: accessToken,
message: message,
link: link
}, function (response) { if (!response || response.error) { console.info('error occured: ' + response.error.message) } else { console.info(' post id: ' + response.id) }; }
);}
In order to post to a Page "as Page", you need to do the following:
Authorize a Page Admin with publish_pages and manage_pages
Use the /me/accounts endpoint to get a Page Token for that Page
Use that Page Token with the /pageid/feed endpoint to post as Page
Make sure you know about the difference between Access Tokens, here are some links about that:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/
Well, I have one website where i'm using the Facebook Plugin (the typical box with your followers and posts)...How i can showing the Facebook Rating in the same box? I'm not found the option :S
It's this possible?
Thanks for help me! And sorry for my English ;)
Edit:
Thanks for the URL's in i'm found one example and i'm editing this. I've the following code:
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/1447162575539510/ratings', function(response) {
if (response && response.error){
console.log("ERROR: "+response.error.message);
}else{
console.log("RESPONSE: "+response.open_graph_story);
//console.log('Successful login for: ' + response.rating);
document.getElementById('status').innerHTML ='Thanks for logging in, ' + response.name + '!';
}
});
//FB.api("/410442505719113/ratings",function(response){
//if (response && !response.error){
//console.log(response.name);
//}else{
//console.log("ERROR : "+response.error.message);
//}
//});
}
But always give me the same error: ERROR: (#210) Subject must be a page. And i don't know for why, the profile is a page in facebook
Here´s what you have to do:
Create a Facebook App
Authorize a page admin with the manage_pages permission
Extend the User Access Token (from 2h to 60 days)
User /me/accounts with that Token to get an Extended Page Access Token
User /{page-id}/ratings with that Page Token to get the ratings
Some links for you, covering all those todos:
https://developers.facebook.com/
https://developers.facebook.com/docs/facebook-login/access-tokens
https://developers.facebook.com/docs/graph-api/reference/v2.1/user/accounts
https://developers.facebook.com/docs/graph-api/reference/v2.1/page/ratings
http://www.devils-heaven.com/facebook-access-tokens/
I have web application where I use Facebook oauth. User authentication works fine. and in javascript when I call FB.getUserID() I get correct identification. But when I try do this I get empty array:
FB.api('/me/albums', function (response) {
console.log(response.data);
console.log(FB.getUserID());
});
What I do wrong?
In order to retrieve the albums created by that user, you must provide user_photos permission to your access token.
Have a look at the this documentation on albums. It clearly says under permission that your Access Token must have the user_photos permission to retrieve the the albums. Without
this specific permission, it'll return an empty object.
Now, in order to provide one or more additional permissions, call FB.login with an option object, and set the scope parameter with a comma-separated list of the permissions you wish to request from the user.
FB.login(function(response) {
// handle the response
}, {scope: 'user_photos,friends_photos'});
Reference: Javascript FB.login doc
In my app, I have my user connect with FB and grant my app permissions.
When they click the Connect button , the FB popup is displayed and they get the
"Login with facebook" and then they need to grand permissions to publish_stream, manage_pages, email.
the problem i am having with my code, is if they remove one of the permissions (like manage_pages ) checkLoginStatus still lets the user continue.
So how to i verify that the user has granted the requested permissions?
<div id="fb-root"></div>
<script src="//connect.facebook.net/en_US/all.js"></script>
<script type="text/javascript">
FB.init({
appId: '<?=FB_APP_ID?>',
status: true,
cookie: true
});
FB.getLoginStatus(checkLoginStatus);
function authUser() {
FB.login(checkLoginStatus, {scope:'publish_stream, manage_pages, email'});
}
function checkLoginStatus(response) {
if(response && response.status == 'connected') {
$('#connbut').hide();
FB.api('/me', function(user) {
if (user) {
$('#fbuser-name').text(user.name);
$('#fbuserpict').attr('src', 'https://graph.facebook.com/' + user.id + '/picture');
$('#fbname').val(user.name);
$('#fbpict').val('https://graph.facebook.com/' + user.id + '/picture');
}
});
} else {
// Display the login button
$('#connbut').show();
}
}
</script>
the problem i am having with my code, is if they remove one of the permissions (like manage_pages ) checkLoginStatus still lets the user continue.
Of course it does, because FB.getLoginStatus says nothing about permissions – it only tells you whether the user is connected to your app or not.
So how to i verify that the user has granted the requested permissions?
You can ask about the permissions the user has currently given your app by requesting /me/permissions (or even just ask for specific permissions using field expansion).
But doing so client-side via JS seems to be cached somehow – asking for permissions, seeing there’s one missing, calling FB.login and asking again will, from my experience, most likely still show the same permissions as before, even if the user has given additional permissions in between. The only way to get reliable information in real time seems to be to do it server-side.
I am trying to work out how to post to a Facebook page wall, when using my app as a different Facebook User (who is not the Page Administrator).
I get a range of error messages while testing:
Exception: 200: The user hasn't authorized the application to perform this action
The page administrator has visited the app and accepted the following permissions: publish_stream, manage_pages, offline_access
Here is the code I plan to use:
// Insert Page Administrators ID here
// This user is not the same user that is currently logged in and using the app
// This user is the page administrator who has authorised:
// - manage_pages
// - offline_access
// - publish_stream
$user_id = '123456789';
// Insert Page ID here
$page_id = '123456789';
$accounts = $facebook->api('/'.$user_id.'/accounts');
foreach($accounts['data'] as $account)
{
if($account['id'] == $page_id)
{
$page_access_token = $account['access_token'];
echo "<p>Page Access Token: $page_access_token</p>";
}
}
// publish to the wall on your page
try
{
$result = $facebook->api(array( "uid" => $page_id,
"method" => "stream.publish",
"access_token" => $page_access_token,
"message" => $message, ));
}
catch (FacebookApiException $e)
{
error_log('FB Error: Could not post on Page Wall. Page ID: ' . $page_id);
error_log('FB Error Message: ' . $e);
}
Note: There may be PHP errors in the code above, as I just spliced it on the fly, but its not so much the PHP errors I need correcting, but more my logically understanding of how I am meant to go about this process.
PROBLEM:
I can't access the $user_id/accounts information without an active user access token for the Page Administrator.
The end result that I'm trying to achieve is:
1.) A normal FB user goes to the app and submits a form
2.) The app posts a message on a FB Page wall, which is not owned by the FB user, but has previously been authorized by the Page Administrator with the following permissions manage_pages, publish_stream and offline_access
Q1. Since the Page Administrator has accepted the appropriate permissions, why can't I just generate an active user access token, without the actual Page Administrator user logging into the website?
Q2. Is there a way I can get the equivalent of /$user_id/accounts for the Page Administrator user_id, when logged into Facebook as a different user (which is why I do not use /me/accounts)?
Q3. Please confirm that my understanding of needing the page access token to post to the page wall is correct (or do I need the user access_token for the Page Administrator - see Q1)?
Q4. Anyone have a handy resource on what each type of access_token can actually access?
If you need any more information, please let me know.
I've spent the last few days working on this and I'm stuck.
Thanks!
You can ask the page admin for manage_pages along with offline_access. I do this in my production app to be able to post scheduled postings onto the pages' walls.
Nope. Not possible. That's what asking permissions is all about. And why not everyone gets to administer everyone else's pages. Could you image if you could administer anyone's page without them granting you access?!?
To post to the page as the page, you need a page access token. To post to page's wall as a user, you need a user access token.
Yes, please see: https://developers.facebook.com/docs/reference/api/permissions and https://developers.facebook.com/docs/authentication/
If you have further questions about any one of these, please start a new question. It's not really fair to users of stackoverflow to be hit with 4 questions in one and then to be asked followup questions to each of those.
I have done in Django:
Step to get Page_access_token:
facebook_page_id=360729583957969
graph = GraphAPI(request.facebook.user.oauth_token.token)
page_access_token=graph.get(facebook_page_id+'?fields=access_token')
This way you can get Page access token.
You can check this thing on Fb GraphAPIexplorer:
http://developers.facebook.com/tools/explorer
GET URL: fb_page_id?fields=access_token
for example: 360729583957969?fields=access_token
that will give you page_access_token