Accounts, Roles, and Publishing New Posts
Learn how to fetch users' pages and roles and how to publish posts on a page.
We'll cover the following
Pages are public profiles that represent different organizations, businesses, celebrities, and public figures. They allow users to view content and like/follow, depending on the user's interests. Pages offer a variety of tools and features for users to share updates and content with their followers. Pages can advertise and promote products. They also provide information about an organization and its cause.
User's pages
To access a user's pages using the Facebook Graph API, we have to make a GET
request to the accounts
edge with the user's ID and a user access token with the pages_show_list
permission. The base URL for the endpoint above is:
https://graph.facebook.com/v16.0/{user_id}/accounts
Note: For the page endpoints to work, we must have a Facebook page. Refer to this lesson to learn how to create a Facebook page.
Request Parameters
Parameter | Type | Category | Description |
| String | Mandatory | This is the token that we received after app authentication and authorization. |
| String | Mandatory | This is the ID of the user. Note that this is a path parameter. |
| String | Optional | This specifies the maximum number of results we want in the response. |
| String | Optional | These are the fields we want in the response from the API call. |
The code below uses the above endpoint. We will extract the page access token and page ID from the response so that it can be used to make API calls related to pages. Click the “Run” button to see the response.
// Importing libraries hereimport fetch from "node-fetch"// Define endpoint URL hereconst endpointUrl = new URL("https://graph.facebook.com/v16.0/{{USER_ID}}/accounts");const headerParameters = {contentType: "application/json",};// Setting API call optionsconst options = {method: "GET",headers: headerParameters,};// Define Query Parameters hereconst queryParameters = new URLSearchParams({access_token: '{{USER_ACCESS_TOKEN}}',limit: '10'});// Function to make API callasync function fetchUserAccounts() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callfetchUserAccounts();
In the code widget above:
Line 5: We define the endpoint URL in the
endpointUrl
variable.Lines 18–21: We add the
access_token
and set thelimit
to10
to specify the maximum number of results to return in response in thequeryParameters
variable.Line 27: We use the
fetch
function to make the API call.
Response Fields
Name | Type | Description |
| String | This is the ID for the page. |
| String | This is the name of the page. |
| String | This token can be used to make requests to the Graph API on behalf of the page. This token will be unique to every page. |
| String | This is the category of the page ("Business," "Government," "Community"). |
| Array | This is the array of permissions that the user has for the page. The terms could be: |
| Object | This is the object that contains the URL of the profile picture of the page. |
| Object | This is the information about the pagination of the feed. It contains fields such as |
| Object | This is a summary of the feed containing information such as |
Roles
There can be a different number of users who manage the page, so roles are used to determine the level of access and permissions a user has. The following are the roles on a Facebook page:
Admin: Admins have complete control over the page and are the highest authority. They can also add/remove other admins.
Editor: Editors have similar permissions to admins, but they can't add/remove admins.
Moderator: Moderators manage the comments and messages on the page. They don't have any other permissions.
Advertiser: Advertisers can create/manage ads for the page. They don't have any additional permissions.
Analyst: Analysts can view the page's insights and analytics. They don't have any other permissions.
The base URL for the above endpoint is:
https://graph.facebook.com/v16.0/{page_id}/roles
Request Parameters
Parameter | Type | Category | Description |
| String | Mandatory | This is the token that we received in response after making the call to the |
| String | Mandatory | This is the ID of the page. Note that this is a path parameter. |
| String | Optional | This specifies the maximum number of results we want in the response. |
To access a page's roles using the Facebook Graph API, we have to make a GET
request to the roles
edge with the page's ID and a page access token. Click the “Run” button to see the response.
// Importing libraries hereimport fetch from "node-fetch"// Define endpoint URL hereconst endpointUrl = new URL("https://graph.facebook.com/v16.0/{{PAGE_ID}}/roles");const headerParameters = {contentType: "application/json",};// Setting API call optionsconst options = {method: "GET",headers: headerParameters,};// Define Query Parameters hereconst queryParameters = new URLSearchParams({access_token: '{{PAGE_ACCESS_TOKEN}}',limit: '10'});// Function to make API callasync function fetchPageRoles() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callfetchPageRoles();
In the code widget above:
Line 5: We define the endpoint URL in the
endpointUrl
variable.Lines 18–21: We add the
access_token
and set thelimit
to10
to specify the maximum number of results to return in response in thequeryParameters
variable.Line 27: We use the
fetch
function to make the API call.
Response Fields
Name | Type | Description |
| String | This is the ID of the user associated with the role. |
| String | This is the name of the user associated with the role. |
| Array | This is the array of tasks that the user has for the page. The possible values are: `ADMINISTER`, `EDIT_PROFILE`, `CREATE_CONTENT`, `MODERATE_CONTENT`, `CREATE_ADS`, `BASIC_ADMIN`, `MANAGE`, and `ASSIGN_PAGE_ROLES`. |
| Object | This is the information about the pagination of the feed. It contains fields such as |
| Object | This is a summary of the feed containing information such as |
Publish new posts
A page post is a piece of content published on a page that is visible to the page's followers and users who visit the page. Businesses, public figures, and organizations share announcements, updates, and other relevant content with their followers and the Facebook users' community by publishing posts on their pages. The base URL for the endpoint above is:
https://graph.facebook.com/v16.0/{page_id}/feed
Request Parameters
Parameter | Type | Category | Description |
| String | Mandatory | This is the token that we received in response after making the call to the |
| String | Optional | These are the fields we want in the response from the API call. |
| String | Mandatory | This is the ID of the page. Note that this is a path parameter. |
| String | Optional | This is the text of the post. This parameter is required if we're publishing a plaintext post. |
| String | Optional | This is the URL that will be included in the post. If we include a link, the |
| String | Optional | This is the name of the link that will be included in the post. |
| String | Optional | This is the caption of the link that will be included in the post. |
| String | Optional | This is the description of the link that will be included in the post. |
| Boolean | Optional | This field is a boolean indicating if the post should be published immediately. If set to |
| String | Optional | This is the time when the post should be published, and it can be set if the post will be published in the future. |
To publish a new post on the page using the Facebook Graph API, we can make a POST
request to the feed
edge of the page
object using the page's ID and a page access token that has the pages_read_engagement
and pages_manage_posts
permissions. The code below uses the endpoint above to publish a post on the page. Click the “Run” button to see the response.
// Importing libraries hereimport fetch from "node-fetch"// Define endpoint URL hereconst endpointUrl = new URL("https://graph.facebook.com/v16.0/{{PAGE_ID}}/feed");const headerParameters = {contentType: "application/json",};// Setting API call optionsconst options = {method: "POST",headers: headerParameters,};// Define Query Parameters hereconst queryParameters = new URLSearchParams({access_token: '{{PAGE_ACCESS_TOKEN}}',message: 'This is a test message',fields: 'id,created_time'});// Function to make API callasync function publishNewPost() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);// Printing responseprintResponse(response);} catch (error) {// Printing error messageprintError(error);}}// Calling function to make API callpublishNewPost();
In the code widget above:
Line 5: We define the endpoint URL in the
endpointUrl
variable.Lines 18–22: We add the
access_token
and themessage
we want to publish in thequeryParameters
variable. We also specify thefields
we want in the response.Line 27: We use the
fetch
function to make the API call.
Response Fields
Name | Type | Description |
| String | This is the ID of the post. |
| String | This is the time the post was created. The time is in ISO 8601 format. |
If the request wasn't successful, the response will contain an error
object, which will include an error code
, message
, and an fbtrace_id
that could be used for debugging purposes.
Note: In order to view the feed, we will make a
GET
request to the same endpoint.