CDP Python Connector

Unlock and extend the value of Data Cloud data with the CDP Python Connector. The connector uses the Query API and extracts data from Data Cloud 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.
    1pip install salesforce-cdp-connector

    Upon successful installation, you’ll see the following message Successfully Installed salesforce-cdp-connector-<version>.

    Note

  • Authenticate the CDP Python Connector. You can authenticate the connector using two ways:
    • Authenticate with username and password
      1. Create your connected app, and complete its basic information.
      2. Select Enable OAuth Settings.
      3. Enter the callback URL (endpoint) that Salesforce calls back to your application during OAuth.
      4. 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.
      5. Click Save for the changes to take effect.
      6. Click Continue to get to Manage Connected Apps.
      7. Copy the consumer key and consumer secret. They’re used during the creation of the Connection Object.
    • Authenticate using OAuth endpoint
      1. Create your connected app, and complete its basic information.
      2. Select Enable OAuth Settings.
      3. Enter the callback URL (endpoint) that Salesforce calls back to your application during OAuth.
      4. 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.
      5. Click Save for the changes to take effect.
      6. Click Continue to get to Manage Connected Apps.
      7. Copy the consumer key and consumer secret. They’re used during the creation of the Connection Object.
      8. 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.
      9. 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.

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

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
    1from salesforcecdpconnector.connection import SalesforceCDPConnection
    2conn = SalesforceCDPConnection(
    3        'login_url', 
    4        'user_name', 
    5        'password', 
    6        'client_id', 
    7        'client_secret'
    8    )
  • 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.
    1from salesforcecdpconnector.connection import SalesforceCDPConnection
    2conn = SalesforceCDPConnection(login_url, 
    3       client_id='<client_id>', 
    4       client_secret='<client_secret>', 
    5       core_token='<core token>'
    6       refresh_token='<refresh_token>'
    7   )

Create a Cursor Object and query Data Cloud

Create a cursor object to execute queries. When a query is executed, the cursor passes on that query to the Data Cloud to fetch the results.
1cur = conn.cursor()
2cur.execute('<query>')

Fetch Data Cloud 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.
    1results = cur.fetchone()
  • Get all the rows:Use the fetchall() method to retrieve all the query results in one call.
    1results = cur.fetchall()
  • Get results in a dataframe: You can retrieve the results into a Pandas DataFrame using the get_pandas_dataframe() method.
    1dataframe = conn.get_pandas_dataframe('<query>')