Monday, April 11, 2011

How to post from facebook app to facebook page?

* NOTE :-

Please follow all the steps carefully and sequentially. Users are advised to follow every step in different tab.


For all the steps we are considering following options :-

1. FB Page :-
Name : Sample Page
{PAGE_ID} : 121364427524405

2. FB App :-
Name : Sample App
{APP_ID} : 303409109772222
{API_KEY} : 222223632fdc6ffffe4a53a453333333
{APP_SECRET} : 1111111111111115f3939c1513ee0844

3. {REDIRECT_URL} :- http:/myexample.com/one.php



In order to post data from App page to other page we have to follow the following steps:-


1.)
synopsis : https://www.facebook.com/dialog/oauth?client_id={APP_ID}&redirect_uri={REDIRECT_URL}&scope=email,read_stream,offline_access,manage_pages


e.g.

https://www.facebook.com/dialog/oauth?client_id=303409109772222&redirect_uri=http://myexample.com/one.php&scope=email,read_stream,offline_access,manage_pages

Redirected to :-
http://myexample.com/one.php?code=60ad2f9f47cdaf5e03176824-100000098944650|W8_4DqYq_KCX_oBaa11oJJQ373Q

Output on redirect URL's screen :
[access_token] =} 303409109772222|60ad2f9f47cdaf5e03176824-100000098944650|xAOzhvZAJELuLgJVouW1l7nrMLQ

2.)
synopsis : https://graph.facebook.com/oauth/access_token?client_id={APP_ID}&redirect_uri={REDIRECT_URL}&client_secret={APP_SECRET}&code={CODE_GENERATED_IN_REDIRECT_TO_URL_IN_STEP_1}


e.g.

https://graph.facebook.com/oauth/access_token?client_id=303409109772222&redirect_uri=http://myexample.com/one.php&client_secret=1111111111111115f3939c1513ee0844&code=60ad2f9f47cdaf5e03176824-100000098944650|W8_4DqYq_KCX_oBaa11oJJQ373Q


Output on redirect URL's screen :
[access_token] =} 303409109772222|60ad2f9f47cdaf5e03176824-100000098944650|xAOzhvZAJELuLgJVouW1l7nrMLQ


3.)
synopsis : https://www.facebook.com/dialog/oauth?client_id={APP_ID}&redirect_uri={REDIRECT_URL}&scope=email,read_stream&response_type=token

e.g.

https://www.facebook.com/dialog/oauth?client_id=303409109772222&redirect_uri=http://myexample.com/one.php&scope=email,read_stream&response_type=token


4.)
synopsis : https://graph.facebook.com/oauth/access_token?client_id={APP_ID}&client_secret={APP_SECRET}&grant_type=client_credentials

e.g.

https://graph.facebook.com/oauth/access_token?client_id=303409109772222&client_secret=1111111111111115f3939c1513ee0844&grant_type=client_credentials

Output on redirect URL's screen :
access_token=303409109772222|ifLwz-k7HwQvG5MfOeg3Y_6fGsQ



5.)
synopsis : https://www.facebook.com/dialog/oauth?client_id={APP_ID}&redirect_uri={REDIRECT_URL}&scope=manage_pages&response_type=token

e.g.

https://www.facebook.com/dialog/oauth?client_id=303409109772222&redirect_uri=http://myexample.com/one.php&scope=manage_pages&response_type=token


6.)
synposis : https://graph.facebook.com/me/accounts?access_token={ACCESS_TOKEN_GENERATED_FROM_STEP_1}

* NOTE : This URL will yield the listing of all pages in JSON format. We will have to find out the ACCESS_TOKEN of that page, on which we have to post.

e.g.

https://graph.facebook.com/me/accounts?access_token=303409109772222|60ad2f9f47cdaf5e03176824-100000098944650|xAOzhvZAJELuLgJVouW1l7nrMLQ


Output on redirect URL's screen :

{"data":[{"name":"Sample Page","category":"Media\/news\/publishing","id":"121364427524405","access_token":"303409109772222|60ad2f9f47cdaf5e03176824-100000098944650|121364427524405|LDf-47eJi-dYgjourPlz4Kp78Jg"}]}


In our case we have to post on Sample page - the first entry in our JSON string. So we will extract access_token parameter of this JSON string for future usage.



7.)
synposis : http://www.facebook.com/login.php?api_key={API_KEY}&connect_display=popup&v=1.0&next={REDIRECT_URL}&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access

e.g.

http://www.facebook.com/login.php?api_key=222223632fdc6ffffe4a53a453333333&connect_display=popup&v=1.0&next=http://myexample.com/one.php&cancel_url=http://www.facebook.com/connect/login_failure.html&fbconnect=true&return_session=true&session_key_only=true&req_perms=read_stream,publish_stream,offline_access

Redirected to :-
http://myexample.com/one.php?session={%22session_key%22%3A%2260ad2f9f47cdaf5e03176824-100000098944650%22%2C%22uid%22%3A100000098944650%2C%22expires%22%3A0%2C%22secret%22%3A%227367a1a32dc7240e8b2fd50c48331c8a%22%2C%22sig%22%3A%22fcf7ce7683a13ffb83a4d96a2aa7f1cb%22}



8.)
curl -F 'access_token={ACCESS_TOKEN_GENERATED_FROM_STEP_6}' \
-F 'message={MESSAGE}' \
-F 'link={HYPER_LINK}' \
-F 'picture={IMAGE}' \
-F 'name={IMAGE_NAME}' \
-F 'caption={IMAGE_CAPTION}' \
-F 'description={DESCRIPTION}' \
-F 'actions={JSON_ENCODED_STRING_FOR_EXTRA_HYPERLINKS}' \
https://graph.facebook.com/{PAGE_ID}/feed

e.g.

curl -F 'access_token=303409109772222|60ad2f9f47cdaf5e03176824-100000098944650|121364427524405|LDf-47eJi-dYgjourPlz4Kp78Jg' \
-F 'message=Message \
-F 'link=http://www.link.com \
-F 'picture=http://link.com/story.jpg' \
-F 'name=Name \
-F 'caption=Caption \
-F 'description=Description \
-F 'actions={"name": "Caption1", "link": "http://link.com"}' \
https://graph.facebook.com/121364427524405/feed

* Note: On success it will return JSON_ENCODED_STRING with post-id as id


Reference:-

http://developers.facebook.com/docs/reference/api/post/

http://www.fbsmm.com/2010/12/04/simple-example-to-post-on-facebook-wall-using-php/

8 comments:

  1. Hi,

    First of all thanks for writing it up and sharing with us.

    Now, I am a PHP dev new to Facebook API and integration so to use that I would need a little more info.

    It would be great and I suppose helpful for other readers if you could answer these questions:

    A) Is it so that the steps 1 to 7 need to be executed just once to allow anyone with access to the app to post on said wall forever?
    Or does it need to be repeated every time you want to publish from the same app to the same page?
    Or does it need to be executed once for each single FB user that will post from the app to the page?

    I tired to wrap it up in an automated PHP sequence to be executed every time someone needs to post from our app to our FB page but it won't work. Already in step 2, when it is executed the second time I get the "Error validating verification code" error.

    B) What is the lifetime of the {ACCESS_TOKEN_GENERATED_FROM_STEP_6}?

    Thanks in advance,
    Sotar.

    ReplyDelete
  2. Hi Jurek,

    In order to post from facebook app to facebook page, you need to follow the mentioned steps only once & sequentially.

    The access_token will persist life long.

    How you automated this? I mean you wrote a script for that?

    ReplyDelete
  3. Hi Jatin,

    Thanks for your reply. Good to know one has to go through this process only once per combination of page-app. This is what I was hoping was the case, wasn't sure.

    Indeed, it was a PHP script that would sequentially run the first few steps, making facebook redirect to the next step, getting responses and passing tokens on.
    I might have made a mistake somewhere perhaps, I am going to give it another try.

    I also don't have much experience with PHP's lib-curl so not sure how to translate the curl command-line notation in your post to the PHP curl functions, but I am sure there are resources out there, I just need to find the right ones.

    Once again thanks for your help.

    ReplyDelete
  4. Hey Jurek,

    Ya, I recommend you that you should follow these steps manually.

    And simply putting these curl commands in backticks i.e. `` will run these commands.

    In fact I did the same in my project.

    ReplyDelete
  5. Thanks for the help, it seems to work now.

    ReplyDelete
  6. how i can get the Output on redirect URL's ???

    ReplyDelete
  7. I need to send notification to user from my app... wt to da???

    ReplyDelete
  8. i am using ASP.net server. what is the equalent to CURL in ASP.net.

    ReplyDelete