Skip to content

Instantly share code, notes, and snippets.

@gjhuerte
Created November 15, 2018 10:04
Show Gist options
  • Save gjhuerte/e837ea96d929943cfdf058ef72cc8dc6 to your computer and use it in GitHub Desktop.
Save gjhuerte/e837ea96d929943cfdf058ef72cc8dc6 to your computer and use it in GitHub Desktop.
posting-in-facebook-api
https://developers.facebook.com/docs/pages/publishing/
@gjhuerte
Copy link
Author

Posting to a Page
With the Pages API, people using your app can post to Facebook as a Page. For example, if a blog editor posts a new article on Facebook, it appears as an article from the blog, not the individual editor. People following the page will see the post in their Newsfeed.

The other key use cases for Facebook Pages are around marketing and advertising for a business using the Marketing API. For more information, see Ads Management API and Unpublished Page Posts

Posting Content
Prerequisites
To publish as a Page from your app, you need:

To be an Admin of the posting Page
A Page access token with manage_pages and publish_pages permissions
Once your app has an access token with the correct permissions, it can post a status update. The update appears in the Page's timeline. Your app should make requests to graph.facebook.com.

POST https://graph.facebook.com/546349135390552/feed
?message=Hello Fans!
&access_token=your-access-token
On success, Graph API responds with JSON containing the Post ID:

{
"id": "546349135390552_1116689038356556"
}
After your app posts Page content, it can delete or update the post.

Sample Request
DELETE https://graph.facebook.com/546349135390552_1116689038356556
?access_token=your-access-token
Sample Response
{
"success": true
}
Links
To post a link, make a POST request. It can contain optional text in the message parameter:

POST https://graph.facebook.com/546349135390552/feed
?message=For all Math geniuses :)
&link=www.projecteuler.net
&access_token=your-access-token
The response contains the Post ID:

{
"id": "546349135390552_1116691191689674"
}
Photos and Videos
If your app posts Page photos, the photos appear in the Page's timeline as well as the Page's photo album. Videos appear in the Page's timeline as well as the Pages | Videos tab.

When you post photos and videos to a page, you add them as edges to existing Graph API nodes. You can also add photos and videos to Users, Albums, Events, and Groups.

For information on video uploads including limitations, supported formats, and ways to upload, see the Video Upload with Graph API Guide.

To post a photo send a request to /page/photos:

POST https://graph.facebook.com/546349135390552/photos
On success, Graph API sends a JSON response with the photo ID and the post ID

{
"id":"1116692661689527",
"post_id":"546349135390552_1116692711689522"
}
To post a video, make a request to /page/videos:

POST https://graph.facebook.com/546349135390552/videos
Graph API sends this response on successful post with the video's object ID:

{
"id":"1116696891689104"
}
Your app can post videos as a chunked, resumable upload enabling you to upload larger videos and more easily handle upload errors and interruptions. For more information, see the Graph API Advanced, Resumable Uploads Guide.

Getting a Post's URL
To get a post's URL or permalink simply append the post ID to https://www.facebook.com/.

https://www.facebook.com/post-id
Calling this URL will redirect the user to the full post URL.

Example:
Assuming your post ID is 1557889584450800_1656388331267591, use the following URL to link to the post:

https://www.facebook.com/1557889584450800_1656388331267591
This will redirect the user to the post full URL:

https://www.facebook.com/page-name/posts/1656388331267591
Comment on a Page Post
There may be cases where your app posts comments on a Page post. As of v2.10 only a Page can comment on a Page post.

Prerequisites
To publish to a comment on a Page post, your app needs:

A Page access token with manage_pages and publish_pages permissions
To be an Admin of the posting Page
The Page post_id of the post on which you wish to comment
Sample Request
POST https://graph.facebook.com/546349135390552_1129650603727066/comments
?message=Nice post!
&access_token=page-access-token
Sample Response
{
"id":"1129650603727066_1139628382729288"
}
If a Page is unpublished only a Page or a Page Admin will be able to comment on an existing post. No one will be able to comment on a Page Admin's comments on a Page post. A Page can comment on a Page's own comments. If you are not the Page or an admin of the Page and try to comment on a comment you will see a 1705 error.

{
"error":{
"message":"(#1705) There was an error posting to this wall",
"type":"OAuthException",
"code":1705,
"fbtrace_id":"HWxq+vf3HsQ"
}
}
Featured Videos and Playlists
When you add a video to a page, you have option to display the video as the Page's featured video or add it to the Page's video playlist.

Publishing Content
When your app publishes content as a Page, you can post it immediately or at a later time. There are different types of Page posts:

Published Content - The default for publishing. This is content someone wants to immediately publish for a page.

Unpublished Content - Any content people want to prepare to post but will wait to make public. The main scenario for this is marketing and advertising content. See Ads Management, Unpublished Page Posts.

Scheduled Publication - Prepare and upload content to post then publish it at a scheduled time.

Create Unpublished Content
To create content to publish at a later time, post the content with the parameter published=false to /page/feed.

Sample Request
POST https://graph.facebook.com/546349135390552/feed
?published=false
&message=An unpublished post
&access_token=your-page-access-token
Sample Response
{
"id": "546349135390552_1116698655022261"
}
Your app should hold on to the id which you can use later to publish the content by setting published=true.

Sample Request
POST https://graph.facebook.com/546349135390552_1116699718355488
?is_published=true
Sample Response
{
"success" : true
}
Schedule Publication
To automatically publish content at a later date, set published to false and use the scheduled_publish_time field to provide the timestamp in any of the following formats:

An integer UNIX timestamp [in seconds] (e.g. 1530432000).
An ISO 8061 timestamp string (e.g. 2018-09-01T10:15:30+01:00).
Any string otherwise parsable by PHP's strtotime() (e.g. +2 weeks, tomorrow)*.
*Note, if you are relying on strtotime()'s relative date strings you can read-after-write the scheduled_publish_time of the created post to make sure it's what is expected.

Sample Request
POST https://graph.facebook.com/546349135390552/feed
?published=false
&message=A scheduled post
&scheduled_publish_time=tomorrow
Sample Response
{
"id": "546349135390552_1588424184546778"
}
Messaging as a Page
Prerequisites
A Page access token with manage_pages and read_page_mailboxes permissions
The user requesting the Page access token must be an admin, editor, or moderator of the page
The conversation_id of the conversation on which you wish to message
To get a list of conversation threads, we send a get request.

Sample Request
GET https://graph.facebook.com/546349135390552/conversations
?fields=id,messages{message}
access_token=your-page_access_token
Sample Response
{
"data": [
{
"id": "t_100023297371310",
"link": "/ashcat2013/inbox/1568830453172818/",
"updated_time": "2018-02-16T18:34:21+0000"
},
{
"id": "t_1634393684",
"link": "/ashcat2013/inbox/1387427367979795/",
"updated_time": "2018-02-16T16:40:15+0000"
},
{
"id": "t_100007408392643",
"link": "/ashcat2013/inbox/1387439687978563/",
"updated_time": "2018-01-16T22:44:45+0000"
}
]
}
Use the conversation_id and provide the message parameter as a POST request.

Sample Request
POST https://graph.facebook.com/t_100023297371310/messages
&message=It is the weekend!
&access_token=your-page_access_token
On success, the API returns the id and the message id:

{
"id": "m_mid.$cAASUMsyJZYFn0SqYclhn-VOMAa4d",
"uuid": "mid.$cAASUMsyJZYFn0SqYclhn-VOMAa4d"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment