Customer Data Platform Python Connector
Unlock and extend the value of Customer Data Platform data with the CDP Python Connector.
The connector uses the Query API and extracts the Customer Data Platform data into Python. It lets
you fetch data in Pandas DataFrames. With the data in your environment, you can create visual data
models, perform powerful analytical operations, or build powerful machine learning and AI models
as well.
Prerequisites
Complete the prerequisites to configure the CDP Python Connector:
- Install the CDP Python Connector from PyPI (Python Package Index)
repository.
- Authenticate the CDP Python Connector. You can authenticate the connector using two ways:
-
Authenticate with username and password
- Create your connected app, and complete its basic information.
- Select Enable OAuth Settings.
- Enter the callback URL (endpoint) that Salesforce calls back to your application during OAuth.
- Select the OAuth scopes to apply to the connected app. OAuth scopes define permissions
for the connected app, which are granted as tokens after the app is authorized. The OAuth
token name is in parentheses.
- Perform ANSI SQL queries on Salesforce CDP data (cdp_query_api): The cdp_query_api scope allows ANSI SQL queries of CDP data on behalf of the user.
- Manage Salesforce CDP profile data (cdp_profile_api): The cdp_profile_api scope allows access to CDP REST API data. Use this scope to manage profile records.
- Perform requests at any time (refresh_token): This scope allows a refresh token to be returned when the requesting client is eligible to receive one. With a refresh token, the app can interact with the user’s data while the user is offline.
- Manage user data via APIs (api): This scope allows access to the current, logged-in user’s account using APIs, such as REST API and Bulk API 2.0.
- Click Save for the changes to take effect.
- Click Continue to get to Manage Connected Apps.
- Copy the consumer key and consumer secret. They’re used during the creation of the Connection Object.
-
Authenticate using OAuth endpoint
- Create your connected app, and complete its basic information.
- Select Enable OAuth Settings.
- Enter the callback URL (endpoint) that Salesforce calls back to your application during OAuth.
- Select the OAuth scopes to apply to the connected app. OAuth scopes define permissions
for the connected app, which are granted as tokens after the app is authorized. The OAuth
token name is in parentheses.
- Perform ANSI SQL queries on Salesforce CDP data (cdp_query_api): The cdp_query_api scope allows ANSI SQL queries of CDP data on behalf of the user.
- Manage Salesforce CDP profile data (cdp_profile_api): The cdp_profile_api scope allows access to CDP REST API data. Use this scope to manage profile records.
- Perform requests at any time (refresh_token): This scope allows a refresh token to be returned when the requesting client is eligible to receive one. With a refresh token, the app can interact with the user’s data while the user is offline.
- Manage user data via APIs (api): This scope allows access to the current, logged-in user’s account using APIs, such as REST API and Bulk API 2.0.
- Click Save for the changes to take effect.
- Click Continue to get to Manage Connected Apps.
- Copy the consumer key and consumer secret. They’re used during the creation of the Connection Object.
- Paste this URL in a browser and it will redirect you to the callback url. The redirected url is of the form<callback url>?code=<CODE>. . Extract the CODE from the address bar.
- Make a post call to <YOUR_ORG_URL>/services/oauth2/token?code=<CODE>&grant_type=authorization_code&client_id=<clientId>&client_secret=<clientSecret>&redirect_uri=<callback_uri> .The response to the post call is a json with access_token and refresh_token.
-
Authenticate with username and password
Use Salesforce CDP Python Connector
The CDP Python Connector setup requires you to:
- Create a Connection Object
- Create a Cursor Object and query Customer Data Platform
- Fetch the Customer Data Platform data
Create a Connection Object
You can create a connection object in two ways:
-
With username and password Use the parameters described here and instantiate a
Connection Object with username and password.
Parameters Description login_url Salesforce org url user_name Salesforce org Username password Salesforce org password client_id consumer key generated by Connected App client_secret consumer secret generated by the Connected App -
With OAuth endpoint Use the parameters described here and instantiate a connection
object with OAuth endpoint.
Parameters Description login_url Salesforce org url client_id Salesforce org Username client_secret Salesforce org password core_token access_token received in step-9 of Authenticate using OAuth endpoint section. refresh_token refresh_token received in step-9 of Authenticate using OAuth endpoint section.
Create a Cursor Object and query Customer Data Platform
Create a cursor object to execute queries. When a query is executed, the cursor passes on that
query to the Customer Data Platform to fetch the results.
Fetch Customer Data Platform data
You can fetch the data in three possible ways:
-
One row at a time: Use the fetchone() method to
retrieve one row of result at a time.
-
Get all the rows:Use the fetchall() method to
retrieve all the query results in one
call.
-
Get results in a dataframe: You can retrieve the results into a Pandas DataFrame
using the get_pandas_dataframe() method.