1

Okay, there maybe many question same to me. But I can not solve how to handle in my case. Let start:

I develop a property listing website. The flowchart is simple. User can posting as many as they want using facebook login.

Now they all are okay. I also can solve about how whenever user finish their posting, it will automatically post to their personal/user wall page.

But I want that every listing should be also posted on MY property listing page. Whether it will post as user who make the posting or as page name, no problem. But I need to make sure it not use my personal/user/admin of the page.

So I can't find any answer how to solve this. How in a single website session, my script can post to the user wall who make the posting, and also post to my page. If it prohibited for user who are a foreigner and not the admin to post to my page, then how to make post to my page under the name of the page itself, when the facebook api created is belong to the user. Do I need to create different facebook api object? Need help..thank you for anyone who can help me. Really appreciated.

Below is the code I have use:

$attachment = array(
                'message' => FB_STREAM_MSG,
                'picture' => $imgsrc,
                'link' => $seourl,
                'name' => $prop_name,
                'caption' => FB_STREAM_CAP,
                'description' => substr($prop_desc,0,150) . '..',
            );
            $facebook->api("/$user/feed", 'POST', $attachment);
            //$facebook->api("/".FB_PAGE_ID, 'POST', $attachment); //this doesn't work.
Benny Chen
  • 21
  • 1
  • 3
  • To post to a page _as_ the page, you will need a page access token. https://developers.facebook.com/docs/facebook-login/access-tokens/ – CBroe Oct 02 '13 at 10:23
  • As far as I know, page access token granted to access page own by user who made the login. does it works also to access my page wall? i intend to make a post to my page but the session/fb api created on web belongs to user. The user who login is a foreigner who might not be me nor have access/admin to the page. – Benny Chen Oct 03 '13 at 08:11
  • I have tried before to add new permission "manage_pages". But it seems it grant the application to manage the page owned by user who made the login,it says nothing about grant access to page owned by me. And code above still didn't work. – Benny Chen Oct 03 '13 at 08:24
  • Of course, only an admin of a page can get a page access token for that page. But if you get one that does not expire as described in the docs, you can save that on your server and use it for those API calls. – CBroe Oct 03 '13 at 09:08
  • Ah. I got it. So I just need to get the page access token at once time and use it later even the api assign to different user. Thanks alot. I have tried it and seems work. But I still have an issue about who make the post. I tried and the post show it being posted by user under recent post by user section. I am expecting it posted by page. – Benny Chen Oct 03 '13 at 10:36
  • Ok..this is very frustating. Just ok few hours ago for once and now it post nothing in my page wall while the facebook->api returning '1' with no error at all, hope it means it post successfully, but nothing post on my page wall... – Benny Chen Oct 03 '13 at 10:59
  • Blame me...its my fault..Missing trailing of '/feed' in api call. Now it works great. It also post as page as well. Progress continue very well. Thanks to @CBroe. Now I face another problem, if save the last page access token I use, and use it again, it doesn't work. It says like this: ..Invalid OAuth access token.. I think it must the the expire or session related issue even I just refresh the page in less then a minute. But I search on google many says page_access_token will last forever as long as the user access token always extended. – Benny Chen Oct 03 '13 at 11:35
  • This will also create another problem, because there may come different and many user make post to my site, and their each user access token will usually expire. I still figure it out how to manage this. Or @Cbroe or anyone else can give suggestion I really appreciated. – Benny Chen Oct 03 '13 at 11:43

1 Answers1

1

Ok...problem all SOLVED!

To POST to other user wall, the user need to sign in to generate user token and code as usually like this:

$attachment = array(
'message' => ClearText_FB($prop_name),
'picture' => $imgsrc,
'link' => $seourl,
'name' => ClearText_FB($prop_name),
'caption' => FB_STREAM_CAP,
'description' => ClearText_FB(substr($prop_desc,0,150) . '..'),
);
$facebook->api("/$user/feed", 'POST', $attachment);

And to post to our own page wall, it doesn't matter who is the user, whether the user is the admin or not of the page. But it matters to get the page access token to grant the application to post to page wall. The fb docs said the page access token will last forever, so we just need to get once and save it for next use.

To get page access token for the first time:

$page_access_token = "";
            $result = $facebook->api("/me/accounts");
            foreach($result["data"] as $page) {
                if($page["id"] == $page_id) {
                    //$page_access_token = $page["access_token"];
                    $page_access_token = $facebook->api("/".FB_PAGE_ID."?fields=access_token");
                    break;
                }
            }

Then save the $page_access_token value to server/db or file. Code above doesnt need anymore. Comment out or just remove it. Remove 'name' field as it seems will place post under other user post section. This will post to page wall as page not as user:

$attachment = array(
'message' => ClearText_FB($prop_name),
'picture' => $imgsrc,
'link' => $seourl,
'description' => ClearText_FB(substr($prop_desc,0,150) . '..'),
'access_token' => $page_access_token,
);
$facebook->api("/".FB_PAGE_ID.'/feed', 'POST', $attachment);
Benny Chen
  • 21
  • 1
  • 3