Created
September 13, 2021 06:02
-
-
Save naveenvm93/465f211a96f99a3eb23463a3a1e7027d to your computer and use it in GitHub Desktop.
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
<script runat = "server" > | |
Platform.Load("Core", "1"); | |
try { | |
//authenticate to get access token | |
var authEndpoint = 'https://xxxxx.auth.marketingcloudapis.com/' //provide API endpoint | |
var payload = { | |
client_id: "xxxxx", //pass Client ID | |
client_secret: "xxxxx", //pass Client Secret | |
grant_type: "client_credentials" //pass MID | |
}; | |
var url = authEndpoint + '/v2/token' | |
var contentType = 'application/json' | |
var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload)); | |
if (accessTokenRequest.StatusCode == 200) { | |
var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]); | |
var accessToken = tokenResponse.access_token //get access token | |
var rest_instance_url = tokenResponse.rest_instance_url //get rest URL | |
}; | |
//make api call to Retrieve Assets | |
if (accessToken != null) { | |
var assetURL = rest_instance_url + "/asset/v1/content/assets?$page=1"; //Initial GET URL with page as 1 | |
var headerNames = ["Authorization"]; | |
var headerValues = ["Bearer " + accessToken]; | |
var assetresponse = HTTP.Get(assetURL, headerNames, headerValues); | |
var finalResponse = Platform.Function.ParseJSON(String(assetresponse.Content)); | |
var assetCount = finalResponse.count; | |
var pageCount = finalResponse.page; | |
var pageSize = finalResponse.pageSize; | |
var iteraterows = Math.ceil(assetCount / pageSize) + 1; //Formula to get how many times we need to call the content API | |
for (var j = 1; j < iteraterows; j++) { | |
var assetURLFinal = rest_instance_url + "/asset/v1/content/assets?$page=" +j; //Iterating the pages dynamically | |
var headerNames = ["Authorization"]; | |
var headerValues = ["Bearer " + accessToken]; | |
var assetresponseFinal = HTTP.Get(assetURLFinal, headerNames, headerValues); | |
var finalResponseFinal = Platform.Function.ParseJSON(String(assetresponseFinal.Content)); | |
for (var i = 0; i < finalResponseFinal.items.length; i++) { | |
var displayName = finalResponseFinal.items[i].assetType.displayName; | |
if (displayName == "Image") { //checking if the type is Image | |
var publishedURL = finalResponseFinal.items[i].fileProperties.publishedURL; | |
var fileName = finalResponseFinal.items[i].fileProperties.fileName; | |
var rows = Platform.Function.UpsertDE("Test_Asset_URL", ["Asset_URL"], [publishedURL], ["Asset_Name"], [fileName]); //upserting the Image URL Link and file name | |
} | |
} | |
} | |
} | |
} catch (error) { | |
Write(Stringify(error)); | |
} </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment