Salesforce Service Account Integrations
Problem
You are building interactive or rendered content in Python or R which needs to access a Salesforce resource using an integration user rather than relying on explicit user interaction in an OAuth flow.
Solution
The examples below illustrates how to use the OAuth integrations feature of Posit Connect to delegate authorization to the data provider. These examples use an OAuth access token which identifies the integration user to call the Salesforce API, allowing content to access private resources.
When publishing the content to Connect make sure the following environment variables are set for the deployed content:
SALESFORCE_DOMAIN
from posit import connect
import requests
import os
= connect.Client()
client
= os.getenv("CONNECT_CONTENT_SESSION_TOKEN")
content_session_token = os.getenv("SALESFORCE_DOMAIN")
salesforce_domain
# Example API endpoint. Returns information about the user identified by the OAuth access token.
# In this case, we expect to see a userinfo that identifies the Client Credentials integration user
# configured in the Salesforce OAuth app.
= "https://login.salesforce.com/services/oauth2/userinfo"
user_info_endpoint
= connect.Client()
client = client.oauth.get_content_credentials(content_session_token).get("access_token")
access_token = {"Authorization": f"Bearer {access_token}"}
headers
= requests.get(user_info_endpoint, headers=headers)
response = response.json()
response_dict = response_dict["name"]
name = f"Hello, {name}!"
greeting
print(greeting)
library(connectapi)
library(dplyr)
library(httr2)
# Obtain a Salesforce OAuth token.
<- function() {
salesforce_token # init Posit Connect API client
<- connect()
client
# Exchange content session token for Service Account OAuth access token.
# Connect makes the content session token available in the
# environment variable `CONNECT_CONTENT_SESSION_TOKEN`.
<- get_oauth_content_credentials(client)
credentials $access_token
credentials
}
# Get information about the user.
<- function() {
salesforce_user <- Sys.getenv("SALESFORCE_DOMAIN")
domain <- salesforce_token()
token <- httr2::request("https://login.salesforce.com/services/oauth2/userinfo") |>
resp ::req_auth_bearer_token(token) |>
httr2::req_retry(max_tries = 2) |>
httr2::req_perform()
httr2<- httr2::resp_body_json(resp)
resp_body
resp_body
}salesforce_user()$name