Guide to Salesforce Data Cloud API Connections

Guide to Salesforce Data Cloud API Connections

In this Blog Post, we are going to see how to connect to the Salesforce Data Cloud API.

Prerequisites:

1. OpenSSL to generate Key and Cert.

2. Salesforce Data Cloud Setup.

3. Python.

Following Steps provide a technical overview and step-by-step guide for connecting to the Salesforce Data Cloud API.

Steps:

1. Using the following command, create a key.

openssl genrsa 2048 > host.key && chmod 400 host.key

2. Use the following command to create the cert.

openssl req -new -x509 -nodes -sha256 -days 365 -key host.key -out host.crt

3. Create an External Client App in Salesforce. Use http://localhost:1717/OauthRedirect as the OAuth Start URL.

4. Use the following OAuth Scopes. Also, enable “Enable JWT Bearer Flow” and upload the Cert.

5. Get the Consumer key from the External Client App.

6. Run the following Python code to get the encoded JWT.

import jwt
import datetime as dt

# Method to encode a JWT using the payload and private key type that the Salesforce APIs expect.
# It returns the encoded JWT.
def encodeJWT(login_url, client_id, username, filename) -> str:

	# Unix timestamp for the current time plus 90 minutes
	current_time = dt.datetime.now( dt.timezone.utc )
	expiry = round( current_time.timestamp() + ( 90 * 60 ) )

	with open( filename ) as f:
		key = f.read()

		# The payload for JWT
		payload = {
			"iss": client_id,
			"sub": username,
			"aud": login_url,
			"exp": expiry
		}

	# Create the JWT.
	encoded = jwt.encode( payload, key, algorithm='RS256' )

	return encoded

returnValue = encodeJWT(
    "https://login.salesforce.com",
    "<CONSUMER_KEY",
    "<SALESFORCE_USERNAME>",
    "<PATH_TO_THE_KEY>"
)

print( returnValue )

7. Do a POST Request to generate the Access Token.

Header:

Body:

8. Do a POST Request to exchange the Access Token to get the Data Cloud Access Token.

Header:

Body:

9. For example, to run a query, use a POST Request.

Header:

Body:

Leave a Reply