A JSON Web Token (JWT) is a standard and secure way to transmit information between parties using JSON. General information about JWTs can be found at jwt.io.
This example uses the PyJWT library. (Other JWT libraries may import properly but cause errors when called.)
jwt_generator.py
import sys
import jwt
import datetime
import hashlib
import hmac
def main(api_key):
if not api_key:
print("Usage: python jwt_generator.py <api_key>")
sys.exit(1)
# Define the key
key = api_key.encode()
# Generate the JWT
payload = {
"iss": "Put any value",
"sub": "Put any value",
"iat": datetime.datetime.utcnow()
}
jwt = jwt.encode(payload, key, algorithm='HS256')
print(f"Authorization: C2C:{jwt}")
if ~name~ == "~main~":
if len(sys.argv) < 2:
print("Usage: python jwt_generator.py <api_key>")
sys.exit(1)
api_key = sys.argv[1]
main(api_key)
Replace:
<API_KEY>
with the API key<REGION>
with the Salesforce region<BASE64_URLENCODED_TDS>
with the base64 URL encoded TDSThe URL in the script below is an example. Yours will be different, and will depend on the data provider string you’re assigned.
import requests
def make_api_call(jwt_token):
api_url = "https://dev.api.salesforce.com/analytics/integration/explore-in-tableau/v1/upload-tds-content/example"
# Create the JSON payload (base64-encoded TDS content here)
json_payload = {
"tdsContent": "<BASE64_URLENCODED_TDS>"
}
headers = {
"Authorization": f"C2C:{jwt_token}",
"x-salesforce-region": "<REGION>",
"Content-Type": "application/json"
}
# Send the POST request
response = requests.post(api_url, json=json_payload, headers=headers)
return response
# Main execution
if __name__ == "__main__":
api_key = "<API_KEY>"
# Generate JWT token
jwt_token = generate_jwt(api_key)
# Make the API call with the JWT token
response = make_api_call(jwt_token)
# Check response status code and handle accordingly
if response.status_code == 201:
print("Resource created successfully (201). No response body.")
location_header = response.headers.get("Location")
if location_header:
print(f"Location Header: {location_header}")
else:
print("Location Header not found.")
else:
print(f"Failed to call API. Status Code: {response.status_code}")