Skip to content

Instantly share code, notes, and snippets.

@edsu
Last active October 21, 2025 21:03
Show Gist options
  • Save edsu/6b0c50886be4e1bc5136f1dc4fad5cd5 to your computer and use it in GitHub Desktop.
Save edsu/6b0c50886be4e1bc5136f1dc4fad5cd5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
#
# This program demonstrates using the Tableau REST API to print out our embedding settings.
# To run it you will need to create a Personal Access Token by:
#
# 1. visiting https://tableau-uat.stanford.edu/
# 2. clicking on your user name in the top right
# 3. select "My Settings"
# 4. scrolling to the "Personal Access Tokens" section
# 5. pick a token name, and copy the token secret
# 6. set TABLEAU_TOKEN_NAME and TABLEAU_TOKEN_SECRET in your environment
#
# More about the Tableau REST API can be learned at:
#
# https://help.tableau.com/current/api/rest_api/en-us/REST/rest_api.htm
#
# /// script
# dependencies = ["requests"]
# ///
import os
import json
from functools import cache
import requests
tableau_url = "https://tableau-uat.stanford.edu"
def main():
print(json.dumps(get("settings/embedding"), indent=2))
def get(path):
site_id, token = get_credentials()
resp = requests.get(
f"{tableau_url}/api/3.26/sites/{site_id}/{path}" ,
headers={
"Authorization": f"Bearer {token}",
"Accept": "application/json"
}
)
resp.raise_for_status()
return resp.json()
@cache
def get_credentials():
token_name = os.environ.get("TABLEAU_TOKEN_NAME")
token_secret = os.environ.get("TABLEAU_TOKEN_SECRET")
resp = requests.post(
f"{tableau_url}/api/3.26/auth/signin",
json={
"credentials": {
"personalAccessTokenName": token_name,
"personalAccessTokenSecret": token_secret,
"site": {
"contentUrl": "DLSS"
}
}
},
headers={
"Accept": "application/json"
},
)
resp.raise_for_status()
data = resp.json()
breakpoint()
return data['credentials']['site']['id'], data['credentials']['token']
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment