Palantir Foundry Integrations with Python
All of the content listed below operates under the assumption that all of the necessary setup in the Palantir Foundry section of the OAuth Integrations Admin Guide has already been completed.
Problem
You are building an interactive Python application which needs to act as the person visiting the application when accessing a private Palantir Foundry resource. The content must authenticate to Palantir Foundry using the viewer’s Foundry credentials. For example, accessing datasets in Palantir Foundry where each content viewer has different permissions based on their Foundry account.
Solution
The example below illustrates how to use the OAuth integrations feature of Posit Connect to delegate authorization to Palantir Foundry. This example uses the viewer’s OAuth access token to call the Foundry SDK on behalf of the content viewer, allowing content to access private Foundry resources as the content viewer.
When publishing the content to Connect make sure the following environment variables are set for the deployed content:
- FOUNDRY_HOSTNAME
- DATASET_RID
Currently, the only supported way to interact with Foundry is through the Palantir SDK, which is available in Python and TypeScript.
Shiny
requirements.txt
shiny>=1.1.0
posit-sdk==0.12.0
foundry-platform-sdk==1.53.0app.py
import os
from posit import connect
from shiny.express import render, session
from foundry_sdk import FoundryClient, UserTokenAuth
HOSTNAME = os.getenv("FOUNDRY_HOSTNAME")
DATASET_RID = os.getenv("DATASET_RID")
connect_client = connect.Client()
@render.text
def dataset_versions():
    user_session_token = session.http_conn.headers.get(
        "Posit-Connect-User-Session-Token"
    )
    token = connect_client.oauth.get_credentials(user_session_token).get("access_token")
    foundry_client = FoundryClient(auth=UserTokenAuth(token), hostname=HOSTNAME)
    # List all versions of a given dataset (i.e. file):
    versions = []
    for item in foundry_client.datasets.Dataset.Branch.list(DATASET_RID):
        versions.append(str(item))
    return "\n".join(versions)