How to post to a Facebook Page (how to get page access token + user access token) - facebook

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

Related

App in facebook Require Approval?

Scenario is like this:
I have created an app in facebook say myApp.
I have integrated facebook login in my website say mywebsite.com.
I am intended to fetch user's "education_history" ,"work_history" ,"books" , "music".
I am not asking permissions for writing anything on user's facebook wall.
Here the problem.
I am able to fetch required information from facebook only when I logged in ( myapp created on my account only ) but when someone else try to login to mywebsite.com through facebook then I get only his public profile nothing else.
Why is it so ? What I am missing. All permissions are correct.
Thanks for help
EDITED :
Code for login URl :
// Return facebook or linkedIn login url
public function loginurl(){
// It is facebook
$login_url_params = array(
'scope' => 'email,user_actions.book,user_actions.fitness,user_actions.music,user_actions.news,user_actions.video,user_education_history,user_groups,user_hometown,user_interests,user_likes,user_website,user_work_history',
'redirect_uri' => 'http://localhost/users/store/fb/'
);
$login_url = $this->facebook->getLoginUrl($login_url_params);
echo "<a href=" . $login_url.'>Login</a>';
}
You should get a warning when you authorize some specific permissions as App Admin/Developer, telling you to review them:
Apps requesting more than public_profile, email and the user_friends permission must be reviewed by Facebook before those permissions can be requested from people
HereĀ“s more information about the review process: https://developers.facebook.com/docs/apps/review
Localhost will only work for you, but not for other users. Except you are trying with another user on your computer, of course.

Facebook access token for post message in timeline

I am trying to do something like this. Lets say a user used FB Connect for registration in our site, so I can get the "uid" of facebook and I can store in DB. Now what I want each time that user will visit a store details page or item details page I will post that store/item image with link, photo, description etc to FB timeline.
Something like this:
$post_id = $facebook->api('/me/feed/', 'post', array(
'message' => $products_name, // item name
'link' => 'http://www.blabla.com/item/myshoes', // item url
'picture' => $fb_img_src, // item image
'caption' => $products_name, // item name for caption
'description' => $products_description // item description
));
} catch (FacebookApiException $e) {
$user = null;
}
This process works fine if user logged in to FB by using our FB app. But as I said I want to post if they not even logged in by using their facebook "uid".
Is it possible to authenticate that user depending on facebook "uid"?
Thanks in advance! Any clue/help will be appreciated!
I strongly believe that posting anything to user's Facebook without their's consent or even their action will surely and quickly encourage them to leave your site forever. Do not do this.
Anyway - to answer your question:
Yes, it is possible to get access to user's Facebook and "do stuff", especially when a user is logged in both on Facebook and on your site. You just need to obtain user's access token (read about it in the docs), and make sure the users grants your app all aproppriate permissions. There are also access tokens that can be used offline (user is not even online), but I'm not going to discuss it here.
One way to obtains user's access token is to redirect the user to FB login url providing your APP_ID (as described in FB developers docs). FB will then redirect the user to your Fb-login URL with access code/access token, which enables you to do something like posting to users timeline.
Of course actions that you can take are limited according to permissions said user has granted for your app. And I have to remind you - it's a thin ice you're stepping on.

Is it possible to publish action from a Facebook Page?

I've been using the great facebook OpenGraph and the publish_actions permission. My users can publish actions through our website, and it's directly connected to there Facebook-Timeline. Great integration, very simple to be developped : thanks a lot for your work!
But, I have one more question :
Is it possible to use publish_actions not from a specific user but directly from a Facebook Page? (as described here for a user: https://developers.facebook.com/docs/opengraph/actions/#create)
For example, User John Smith has one Facebook Page (named "John Smith Advocate"), and wants to publish action, not from John Smith himself, but from the Facebook Page "John Smith Advocate".
I wonder if it's possible.
Thanks a lot guys !
In order to publish as a page, you need to obtain an authentication token for that page. To do this you need to follow these steps (sorry for the lack of detail, hopefully this will point you in the right direction):
1) A user who is an administrator of your page needs to be authenticated with your app, and you need to get the "manage_pages" extended permission for them
2) get a list of all the accounts the user is an admin of:
https://graph.facebook.com/me/accounts
3) you'll get a json array of pages the user is an admin of, and also an access token for each page - extract the relevant access token
4) Use the access token to post to the relevant album ID (owned by the page) - here is some php code that does it;
<?php
$args = array('message' => 'Caption');
$args['image'] = '/path/to/image.jpg';
$args['access_token'] = $page_access_token;
$data = $facebook->api('/'. $album_id . '/photos', 'post', $args);

redirect to login page of website, if a user denies facebook permissions

I am implementing a facebook integration to a website. If user denies facebook permissions, how can I redirect them back to my website?
As #abraham has already mentioned that user denial also redirects to your redirect_uri, all you have to do is check if the user has denied permissions.
The following is how facebook redirects to your url, i.e. the redirect_uri , when the user has denied permissions.
http://
YOUR_URL?error_reason=user_denied&
error=access_denied&error_description=The+user+denied+your+request.
So in your code you can check if the error and/or error_reason and/or error_description fields are set in the url, and then do whatever specific actions you want to do when user denies.
Some code to help you out:
if(isset($_REQUEST['error'])){//ok some error has occurred from Facebook
if(isset($_REQUEST['error_reason']) && $_REQUEST['error_reason']=='user_denied'){// ok so the error has occurred coz the user denied permissions!
// a lot of apps render a static page explaining why it needs the permission it has asked for, you can do the same here!
// more code
}
}
// rest of you code ...
Hope this helps.
Technically, if the user is still on your site you can do that but if on facebook's site, then they can automatically provide redirect instruction in their codes. implementing this should give you a basic idea on how to get the user back to your site.
$siteurl = 'http://www.mysiteexample.com';
if ( isset( $_REQUEST['redirect_to'] ) ) {
$redirect_to = $_REQUEST['redirect_to'];
} else {
//
$redirect_to = 'siteurl' .'/'.$_SERVER["REQUEST_URI"];
}
According to Facebook's authentication page. If they user denies access they will get redirected to your redirect_uri with an error. You can check if the error is there and redirect to the homepage.
If the user presses Don't Allow, your app is not authorized. The OAuth Dialog will redirect (via HTTP 302) the user's browser to the URL you passed in the redirect_uri parameter with the following error information:
https://developers.facebook.com/docs/authentication/

How to make a post to a Facebook page?

I have a Facebook page at https://www.facebook.com/xxxx and a web site at http://xxxx.com. How do I make a status update to the facebook page to correspond with every new posting on the actual web site? That update should be authored by the page, and not by my personal Facebook account.
I know it might seem like this is a duplicate question, but hear me out. I have been working on this for hours. I've found a lot of help on this topic, but all of it is incomplete or out of date. Most answers tell me what I need to do, but not how to do it.
The closest thing I could find to what I'm looking for is this note in the official docs.
https://developers.facebook.com/docs/opengraph/#publishing
Here's what I've been able to do so far. I created the facebook page. I created a facebook application. I added the app to the page. I added the app to my personal account and granted it the manage_pages permission. I tried to grant it manage_pages permission from the perspective of the page user, but that doesn't seem to be possible.
I can use this to get an access key for the application:
curl -s -F grant_type=client_credentials -F client_id=APPID -F client_secret=APPSECRET https://graph.facebook.com/oauth/access_token
Then I try to do this to actually make a status update.
curl -s -F access_token=ACCESS_TOKEN -F message='test' https://graph.facebook.com/xxxx/feed
This returns a message that the user hasn't authorized the application to perform this action. I assume that's because I'm trying to post as the page user and not as my personal user.
How can I get the correct access key to make a post on the page as the page user? Also, how can I be sure that access key won't expire? Once I have the access key, how do I actually make the post so that it shows up in the right place?
Here's how the manage_pages permission works.
Bob has a page called Awesemo.
Bob wants UltraPageManager5000 to
manage his page.
Bob goes to a page that UltraPageManager5000 has setup that grants UltraPageManager5000 the manage_pages, publish_stream, and offline_access permissions (just like a normal connect app). You must make sure you ask for offline_access, otherwise the access token will expire.
UltraPageManager5000 now has an access token for Bob, but not for his pages.
Now, when UltraPageManager5000 wants to do anything with one of his pages, they go to https://graph.facebook.com/uid/accounts/?access_token=bobs_access_token and look for the "Awesome-O" page to get the proper access token.
I've made a method that simplifies this a little bit for you.
function get_page_access_token($page_id, $access_token, $user_id) {
$data = file_get_contents('https://graph.beta.facebook.com/'.$user_id.'/accounts?access_token='.$access_token);
$pages = json_decode($data,true);
foreach($pages['data'] as $page) {
if($page['id'] == $page_id) {
return $page['access_token'];
}
}
}
Once you have the actual access token, the following HTTP POST will actually create the status update.
curl -F access_token="the token you got for the page from the command above" \
-F message="the status update you want to post" \
https://graph.facebook.com/PAGE_ID/feed