Facebook Getting the accounts of a user

Getting the accounts of a user

In the accounts endpoint, the GetAccounts method lets you get a list of accounts (pages) the authenticated user has access to - including an access token to act on behalf of each page. An example on how to use the GetAccounts method can be found below:

@using Skybrud.Social.Facebook.Objects.Pages
@using Skybrud.Social.Facebook.Responses.Accounts
@inherits WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{

    // Make the request to the API
    FacebookGetAccountsResponse response = Model.Accounts.GetAccounts();

    // Loop through the accounts
    foreach (FacebookPage account in response.Body.Data) {

        // Write some of the account information to the output stream
        <p>ID: @account.Id</p>
        <p>Name: @account.Name</p>
        <p>Access Token: @account.AccessToken</p>
        <hr />

    }

}

Each account is represented with the FacebookPage class, which has even more properties that the ones shown above.

Fields

By default, only the AccessToken, Category, CategoryList, Name, Id and Permissions properties are included in the response from the Graph API. To get even more properties/fields from the Graph API, have a look at the example below:

@using Skybrud.Social.Facebook.Constants
@using Skybrud.Social.Facebook.Objects.Pages
@using Skybrud.Social.Facebook.Options.Accounts
@using Skybrud.Social.Facebook.Responses.Accounts
@inherits WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{

    // Make the request to the API
    FacebookGetAccountsResponse response = Model.Accounts.GetAccounts(new FacebookGetAccountsOptions {
        Fields = FacebookPageFields.Name + FacebookPageFields.Description
    });

    // Loop through the accounts
    foreach (FacebookPage account in response.Body.Data) {

        // Write some of the account information to the output stream
        <p>ID: @account.Id</p>
        <p>Name: @account.Name</p>
        <p>Description: @account.Description</p>
        <hr />

    }

}

If you have a look at this example, an instance of FacebookGetAccountsOptions is specified as the first parameter for the GetAccounts method. The options class holds a number of properties - eg. the Fields property for specifying the fields/properties that should returned by the API.

Also notice that the FacebookPageFields class contains constants for the supported fields of a Facebook page.

Pagination

While the a standard request to the me/accounts endpoint only will return a certain number of accounts/pages by default, you can also specify the maximum amount of accounts to be included in each response - and also which page that should be returned:

@using Skybrud.Social.Facebook.Objects.Pages
@using Skybrud.Social.Facebook.Options.Accounts
@using Skybrud.Social.Facebook.Responses.Accounts
@inherits WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{

    // The options for the first request to the API
    FacebookGetAccountsOptions options = new FacebookGetAccountsOptions {
        Limit = 2
    };

    // Make the request to the API
    FacebookGetAccountsResponse response1 = Model.Accounts.GetAccounts(options);

    // Loop through the accounts
    foreach (FacebookPage account in response1.Body.Data) {

        // Write some of the account information to the output stream
        <p>ID: @account.Id</p>
        <p>Name: @account.Name</p>
        <hr />

    }

    // Update the options for the second request to the API (so we get the next page)
    options.After = response1.Body.Paging.Cursors.After;

    // Make the request to the API
    FacebookGetAccountsResponse response2 = Model.Accounts.GetAccounts(options);

    // Loop through the accounts
    foreach (FacebookPage account in response2.Body.Data) {

        // Write some of the account information to the output stream
        <p>ID: @account.Id</p>
        <p>Name: @account.Name</p>
        <hr />

    }

}

Facebook uses a cursor-based pagination - which means that with a request (eg. to the first page) to the /me/accounts endpoint, will result in a before cursor and an after cursor. The before refers to the previous page, while the after cursor then refers to the previous page. Notice that Facebook will return these tokens even if you are at either the first or the last page respectively.

Summary

Given how the cursor-based pagination above works, there isn't a way to get the amount of pages. You can however request a summary about the accounts, so you can get the total amount of accounts, and then divided this number by your desired limit (notice that Limit is set to 0):

@using Skybrud.Social.Facebook.Options.Accounts
@using Skybrud.Social.Facebook.Responses.Accounts
@inherits WebViewPage<Skybrud.Social.Facebook.FacebookService>

@{

    // The options for the first request to the API
    FacebookGetAccountsOptions options = new FacebookGetAccountsOptions {
        Limit = 0,
        IncludeSummary = true
    };

    // Make the request to the API
    FacebookGetAccountsResponse response = Model.Accounts.GetAccounts(options);

    <p>Total accounts/pages: @response.Body.Summary.TotalCount</p>

}