Java Example


Generating the JWT

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 JJWT library.

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.time.Instant;
import java.util.Date;

public class JwtGenerator {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("Usage: java JwtGenerator <api_key>");
            System.exit(1);
        }

        String apiKey = args[0];
        
        // Define the key
        final Key key = Keys.hmacShaKeyFor(apiKey.getBytes());
        
        // Generate the JWT
        final String jwt = Jwts.builder()
            .setHeaderParam("typ", "JWT")
            .setIssuer("Put any value")
            .setSubject("Put any value")
            .setIssuedAt(Date.from(Instant.now())) // Use UTC for issued-at timestamp
            .signWith(key)
            .compact();
        
        System.out.println("Authorization: C2C:" + jwt.toString());
    }
}

Sending the request

Replace:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class ExploreInTableau {
    public static void main(String[] args) throws Exception {
        String apiKey = "<API_KEY>";

        // Generate JWT using script from above
        String jwtToken = generateJwt(apiKey);
        HttpResponse<String> response = makeApiCall(jwtToken);

        if (response.statusCode() == 201) {
            System.out.println("Resource created successfully (201). No response body.");

            String locationHeader = response.headers().map().get("Location").get(0);
            if (locationHeader != null) {
                System.out.println("Location Header: " + locationHeader);
            } else {
                System.out.println("Location Header not found.");
            }
        } else {
            System.out.println("Failed to call API. Status Code: " + response.statusCode());
        }
    }
    
    public static HttpResponse<String> makeApiCall(String jwtToken) throws Exception {
        // Create an HTTP client (Java 11 and above)
        HttpClient client = HttpClient.newHttpClient();
    
        String apiUrl = "https://api.salesforce.com/analytics/integration/explore-in-tableau/v1/upload-tds-content/example";
    
        // Create the JSON payload (base64-encoded TDS content here)
        String jsonPayload = "{\"tdsContent\":\"<BASE64_URLENCODED_TDS>\"}";
    
        // Create the POST request with the necessary headers
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(apiUrl))
                .header("Authorization", "C2C:" + jwtToken)
                .header("x-salesforce-region", "<REGION>")
                .header("Content-Type", "application/json")
                .POST(HttpRequest.BodyPublishers.ofString(jsonPayload))
                .build();
    
        return client.send(request, HttpResponse.BodyHandlers.ofString());
    }
}