CDP Python Connector
Prerequisites
- Install the CDP Python Connector from the PyPI (Python Package Index)
repository.
pip install salesforce-cdp-connector
- Authenticate the CDP Python Connector. You can authenticate the connector in one of
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.
- 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. Ensure that refresh_token,
api, cdp_query_api, and cdp_profile_api are selected.
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.
- 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.
- Construct the URL
<LOGIN_URL>/services/oauth2/authorize?response_type=code&client_id=<client_id>&redirect_uri=<callback_url>.
-
To get the <Login_URL>, go to
. -
To get the <callback_url>, go to
.
-
- Paste the URL in a browser and it redirects 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 by using curl or postman 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 CDP Python Connector
The CDP Python Connector setup requires you to:
- Create a Connection Object
- Create a Cursor Object and query Data Cloud
- Fetch the Data Cloud data
Create a Connection Object
-
With username and password: Use the parameters described here, and instantiate
a Connection Object with your 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 from salesforcecdpconnector.connection import SalesforceCDPConnection conn = SalesforceCDPConnection( 'login_url', 'user_name', 'password', 'client_id', 'client_secret' )
-
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 by using the OAuth endpoint section refresh_token refresh_token received in step 9 of Authenticate by using the OAuth endpoint section from salesforcecdpconnector.connection import SalesforceCDPConnection conn = SalesforceCDPConnection(login_url, client_id='<client_id>', client_secret='<client_secret>', core_token='<core token>' refresh_token='<refresh_token>' )
Create a Cursor Object and query Data Cloud
cur = conn.cursor()
cur.execute('<query>')
Fetch Data Cloud data
-
One row at a time: Use the fetchone()
method to retrieve one row of result at a time.
results = cur.fetchone()
-
Get all the rows:Use the fetchall() method
to retrieve all the query results in one
call.
results = cur.fetchall()
-
Get results in a dataframe: You can retrieve the results into a Pandas
DataFrame using the get_pandas_dataframe() method.
dataframe = conn.get_pandas_dataframe('<query>')