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.

A Facebook page
A Facebook page

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

access_token

String

Mandatory

This is the token that we received after app authentication and authorization.

user_id

String

Mandatory

This is the ID of the user. Note that this is a path parameter.

limit

String

Optional

This specifies the maximum number of results we want in the response.

fields

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.

Press + to interact
// Importing libraries here
import fetch from "node-fetch"
// Define endpoint URL here
const endpointUrl = new URL("https://graph.facebook.com/v16.0/{{USER_ID}}/accounts");
const headerParameters = {
contentType: "application/json",
};
// Setting API call options
const options = {
method: "GET",
headers: headerParameters,
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
access_token: '{{USER_ACCESS_TOKEN}}',
limit: '10'
});
// Function to make API call
async function fetchUserAccounts() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
fetchUserAccounts();

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 the limit to 10 to specify the maximum number of results to return in response in the queryParameters variable.

  • Line 27: We use the fetch function to make the API call.

Response Fields

Name

Type

Description

id

String

This is the ID for the page.

name

String

This is the name of the page.

access_token

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.

category

String

This is the category of the page ("Business," "Government," "Community").

tasks

Array

This is the array of permissions that the user has for the page. The terms could be: ADMINISTER, EDIT_PROFILE, CREATE_CONTENT, MODERATE_CONTENT, CREATE_ADS, BASIC_ADMIN, or MANAGE.

picture

Object

This is the object that contains the URL of the profile picture of the page.

paging

Object

This is the information about the pagination of the feed. It contains fields such as next and previous, which we can use to retrieve the next and previous pages of the feed, respectively.

summary

Object

This is a summary of the feed containing information such as total_count (number of total posts in the feed) and can_like (indicating whether or not the feed can be liked).

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

access_token

String

Mandatory

This is the token that we received in response after making the call to the accounts edge of the `user` object.

page_id

String

Mandatory

This is the ID of the page. Note that this is a path parameter.

limit

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.

Press + to interact
// Importing libraries here
import fetch from "node-fetch"
// Define endpoint URL here
const endpointUrl = new URL("https://graph.facebook.com/v16.0/{{PAGE_ID}}/roles");
const headerParameters = {
contentType: "application/json",
};
// Setting API call options
const options = {
method: "GET",
headers: headerParameters,
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
access_token: '{{PAGE_ACCESS_TOKEN}}',
limit: '10'
});
// Function to make API call
async function fetchPageRoles() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
fetchPageRoles();

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 the limit to 10 to specify the maximum number of results to return in response in the queryParameters variable.

  • Line 27: We use the fetch function to make the API call.

Response Fields

Name

Type

Description

id

String

This is the ID of the user associated with the role.

name

String

This is the name of the user associated with the role.

tasks

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`.

paging

Object

This is the information about the pagination of the feed. It contains fields such as next and previous, which we can use to retrieve the next and previous pages of the feed, respectively.

summary

Object

This is a summary of the feed containing information such as total_count (number of total posts in the feed) and can_like (indicating whether or not the feed can be liked).

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

access_token

String

Mandatory

This is the token that we received in response after making the call to the accounts edge of the `user` object.

fields

String

Optional

These are the fields we want in the response from the API call.

page_id

String

Mandatory

This is the ID of the page. Note that this is a path parameter.

message

String

Optional

This is the text of the post. This parameter is required if we're publishing a plaintext post.

link

String

Optional

This is the URL that will be included in the post. If we include a link, the message parameter isn't required.

name

String

Optional

This is the name of the link that will be included in the post.

caption

String

Optional

This is the caption of the link that will be included in the post.

description

String

Optional

This is the description of the link that will be included in the post.

published

Boolean

Optional

This field is a boolean indicating if the post should be published immediately. If set to false, the post will be saved as a draft; its default is true.

scheduled_publish_time

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.

Press + to interact
// Importing libraries here
import fetch from "node-fetch"
// Define endpoint URL here
const endpointUrl = new URL("https://graph.facebook.com/v16.0/{{PAGE_ID}}/feed");
const headerParameters = {
contentType: "application/json",
};
// Setting API call options
const options = {
method: "POST",
headers: headerParameters,
};
// Define Query Parameters here
const queryParameters = new URLSearchParams({
access_token: '{{PAGE_ACCESS_TOKEN}}',
message: 'This is a test message',
fields: 'id,created_time'
});
// Function to make API call
async function publishNewPost() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
// Printing response
printResponse(response);
} catch (error) {
// Printing error message
printError(error);
}
}
// Calling function to make API call
publishNewPost();

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 the message we want to publish in the queryParameters variable. We also specify the fields we want in the response.

  • Line 27: We use the fetch function to make the API call.

Response Fields

Name

Type

Description

id

String

This is the ID of the post.

created_time

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.