FireDAC Components for Facebook Ads

Build 22.0.8462

Establishing a Connection

You can create and manage connections with the Data Explorer and the FireDAC Connection Editor wizards. See Connection Definitions for more information on creating connections from code and creating persistent, private, or temporary connections.

Connecting to Facebook Ads

The following are optional connection properties:

  • Target: Some Facebook tables can be filtered by a target. For example, to retrieve comments on a video, specify the Id of the video as the target. This property enables you to restrict the results of all queries in the connection to records that match the specified target. You can also specify this restriction per query with the Target column.
  • AggregateFormat: The component returns some columns as a string aggregate. For example, the available likes data for an entity is returned in aggregate. By default, the component returns aggregate columns in JSON. You can also return aggregates in XML.
  • Version: Set this property to the Facebook Ads API version if you need to work with a different version than the default.
  • RetryLevel: Use this property to control automatic query retry for specific ad insights queries and errors.

Authenticating to Facebook Ads

Facebook Ads uses the OAuth standard to authenticate users.

OAuth

In all OAuth flows, you must set AuthScheme to OAuth. The sections below assume that you have done so.

Desktop Applications

CData provides an embedded OAuth application that simplifies OAuth desktop Authentication. Alternatively, you can create a custom OAuth application. See Creating a Custom AzureAD App for information about creating custom applications and reasons for doing so.

For authentication, the only difference between the two methods is that you must set two additional connection properties when using custom OAuth applications. After setting the following connection properties, you are ready to connect:

  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
  • OAuthClientId: (custom applications only) Set this to the client Id in your application settings.
  • OAuthClientSecret: (custom applications only) Set this to the client secret in your application settings.
  • CallbackURL: Set this to the Redirect URL in your application settings.
  • Scope (optional): Set this if you need to customizie the permissions that the driver requests.
  • AuthenticateAsPage (optional): Set this to a page name or Id to make requests as a page. The page must be managed by the authenticated user.

When you connect the component opens the OAuth endpoint in your default browser. Log in and grant permissions to the application. The component then completes the OAuth process:

  1. Extracts the access token from the callback URL.
  2. Obtains a new access token when the old one expires.
  3. Saves OAuth values in OAuthSettingsLocation. These values persist across connections.

Web Applications

When connecting via a Web application, you need to create and register a custom OAuth application with Facebook Ads. See Creating a Custom OAuth App for more information about custom applications. You can then use the component to get and manage the OAuth token values. Get an OAuth Access Token

Set the following connection properties to obtain the OAuthAccessToken:

  • AuthScheme: Set this to AzureAD.
  • OAuthClientId: Set this to the client Id in your application settings.
  • OAuthClientSecret: Set this to the client secret in your application settings
  • Scope (optional): Set this if you need to customizie the permissions that the driver requests.
  • AuthenticateAsPage (optional): Set this to a page name or Id to make requests as a page. The page must be managed by the authenticated user.

Then call stored procedures to complete the OAuth exchange:

  1. Call the GetOAuthAuthorizationURL stored procedure. Set the AuthMode input to WEB and set the CallbackURL input to the Redirect URI you specified in your app settings. If necessary, set the "Permissions" parameter to request custom permissions.
  2. Open the URL, log in, and authorize the application. You are redirected back to the callback URL.
  3. Call the GetOAuthAccessToken stored procedure. Set the AuthMode input to WEB. Set the Verifier input to the "code" parameter in the query string of the callback URL. If necessary, set the "Permissions" parameter to request custom permissions.

To connect to data, set the OAuthAccessToken connection property to the access token returned by the stored procedure. When the access token expires after ExpiresIn seconds, call GetOAuthAccessToken again to obtain a new access token.

Requesting Additional Permissions

You may find while using the component that Facebook returns an error stating your app does not have permissions to do a certain action. To resolve this, you must generate a new OAuth access token with the required permissions. Set the Scope property in the authentication step for a desktop application.

Web applications need to call the GetOAuthAuthorizationURL and GetOAuthAccessToken stored procedures, which have inputs for the permissions you would like to request.

Note that these are comma-separated lists of permissions, so you can request more than one per authorization request. You can find a list of available Facebook permissions here:

http://developers.facebook.com/docs/authentication/permissions/

AuthenticateAsPage Property

Use the AuthenticateAsPage connection property if you want to post as a single page. To query collections of pages, leave AuthenticateAsPage blank, in which case CData tools automatically detect which page tokens to use.

The following sections compare the two options.

Posting as a Page

After authenticating to Facebook Ads with your user account, you can post, etc. as one of the pages you manage: Set the AuthenticateAsPage property to the Id of the page you want. You can find the Ids for all pages your account has access to by querying the Pages view.

Automatic Page

Facebook Ads has made a number of recent changes that require page tokens for most resources owned by a page. This can be troublesome if you manage multiple pages and want to execute the same queries across all pages (such as retrieving Insights). In order to make this work seamlessly with our tools, we have added a way to automatically detect the page token to use. For this to work, simply do not specify the AuthenticateAsPage. Note that the correct page token can only be resolved if the page id is specified as part of the target in the request. This means for some requests you will still need to manually specify AuthenticateAsPage.

Using the FireDAC Connection Editor

Complete the following steps to use the FireDAC Connection Editor to assign a Facebook Ads connection to a TFDConnection object:

  1. Open a project and drop a TFDPhysCDataFacebookAdsDriverLink from the Tool Palette onto the Form Designer.
  2. Drop a TFDConnection object onto the Form Designer.
  3. Double-click the TFDConnection and select CDataFacebookAds in the Driver Id menu.
  4. Define the necessary connection properties.
  5. To execute ad-hoc SQL statements based on this connection, enter SQL commands on the SQL Script tab.
  6. In the Form Designer, select the TFDConnection object and set the Connected property to true.

Using the Data Explorer

Complete the following steps to use the Data Explorer to define persistent connections that can be shared across applications and projects:

  1. Click View > Tool Windows > Data Explorer in RAD Studio and expand the FireDAC node.
  2. Right-click the CData Facebook Ads Data Source node and click Add New Connection.
  3. Enter a name for the connection. The FireDAC Connection Editor opens.

Working with Persistent Connections

The connections you define in the Data Explorer can be assigned to a TFDConnection object by specifying the connection name in the TFDConnection object's ConnectionDefName property.

Browsing Data and Metadata

The Data Explorer also provides options for browsing Facebook Ads objects at design time:

  • Expand the CData Facebook Ads Data Source node in the Data Explorer to view the defined connections.
  • Expand a connection node to find the database objects available for a connection.
  • Drill down to a database object to find metadata such as primary keys.
  • To display data, double-click a table or view or right-click and click View.

Copyright (c) 2023 CData Software, Inc. - All rights reserved.
Build 22.0.8462