Created
November 25, 2024 14:22
-
-
Save Mefistophell/ac8ddabe40b0bfad3acb1c22e47dd8ce to your computer and use it in GitHub Desktop.
SFMC: Create/Update/Get FileTransferLocation GCP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as dotenv from 'dotenv' | |
dotenv.config() | |
const baseUrl = process.env.SFMC_BASE_URL as string | |
const tokenUrl = process.env.SFMC_TOKEN_URL_V2 as string | |
const clientId = process.env.SFMC_CLIENT_ID as string | |
const clientSecret = process.env.SFMC_CLIENT_SECRET as string | |
const credentials = { | |
'type': 'service_account', | |
'project_id': '', | |
'private_key_id': '', | |
'private_key': '' | |
'client_email': '', | |
'client_id': '', | |
'auth_uri': '', | |
'token_uri': '', | |
'auth_provider_x509_cert_url': '', | |
'client_x509_cert_url': '' | |
'universe_domain': 'googleapis.com' | |
} | |
async function getAccessToken() { | |
const response = await fetch(tokenUrl, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
client_id: clientId, | |
client_secret: clientSecret, | |
grant_type: 'client_credentials' | |
}) | |
}) | |
if (!response.ok) { | |
throw new Error(`Failed to get access token: ${response.statusText}`) | |
} | |
const data = await response.json() | |
return data.access_token | |
} | |
async function createGcpFileTransferLocation() { | |
const accessToken = await getAccessToken() | |
const gcpFileTransferLocation = { | |
fileTransferLocation: { | |
'name': 'GCP TEST FILE TRANSFER LOCATION', | |
'description': 'GCP TEST FILE TRANSFER LOCATION', | |
'customerKey': 'GCP_TEST_FILE_TRANSFER_LOCATION', | |
'locationType': 'GcpBlobStorage', | |
'gcpFileTransferLocation': { | |
authType: 'CredentialFile', | |
bucketName: 'test_bucket', | |
credentials | |
} | |
} | |
} | |
const response = await fetch(`${baseUrl}/data/v1/filetransferlocation`, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${accessToken}` | |
}, | |
body: JSON.stringify(gcpFileTransferLocation) | |
}) | |
if (!response.ok) { | |
console.error('Error creating GCP File Transfer Location:', response.statusText, await response.text()) | |
return | |
} | |
const result = await response.json() | |
console.log('GCP File Transfer Location created successfully:', result) | |
} | |
async function updateGcpFileTransferLocation() { | |
const accessToken = await getAccessToken() | |
const gcpFileTransferLocation = { | |
fileTransferLocation: { | |
'name': 'test-file-transfer-location', | |
'description': 'test-file-transfer-location', | |
'customerKey': 'test-file-transfer-location', | |
'locationType': 'GcpBlobStorage', | |
'gcpFileTransferLocation': { | |
'relativePath': 'file_transfer_global', | |
authType: 'CredentialFile', | |
bucketName: 'ingka-ime-campman-cicd-import', | |
credentials | |
} | |
} | |
} | |
const response = await fetch(`${baseUrl}/data/v1/filetransferlocation/test-file-transfer-location`, { | |
method: 'PATCH', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${accessToken}` | |
}, | |
body: JSON.stringify(gcpFileTransferLocation) | |
}) | |
if (!response.ok) { | |
console.error('Error updating GCP File Transfer Location:', response.statusText, await response.text()) | |
return | |
} | |
const result = await response.json() | |
console.log('GCP File Transfer Location updated successfully:', result) | |
} | |
async function getGcpFileTransferLocation() { | |
const accessToken = await getAccessToken() | |
const response = await fetch(`${baseUrl}/data/v1/filetransferlocation/GCP_TEST_FILE_TRANSFER_LOCATION`, { | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': `Bearer ${accessToken}` | |
} | |
}) | |
if (!response.ok) { | |
console.error('Error getting GCP File Transfer Location:', response.statusText, await response.text()) | |
return | |
} | |
const result = await response.json() | |
console.log('GCP File Transfer Location:', result) | |
} | |
// getGcpFileTransferLocation() | |
// createGcpFileTransferLocation() | |
updateGcpFileTransferLocation() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment