I’m trying to search for and download an image from canto.com and I need to somehow authenticate within the script first. The first line of shellscript curl code returns an access token. The next two lines return an error with a bunch of HTML code that says something about an insecure content policy and account doesn’t exist. If I paste just the URL from each of the last two lines, it does search for an image and return a bunch of information in the web browser and it does download an image.
I’m not doing something right with the authenticating. This is the documentation page I’m reading Canto API
I’m going to do more with the code in AppleScript once I can figure out how to authenticate so it will search for and download images from canto.com.
--Trying to figure out how to authenticate
do shell script "curl --location -g --request POST 'https://oauth.canto.com/oauth/api/oauth2/compatible/token?app_id=abc defg123456&app_secret=lmnopqr123456789&grant_type=client_credentials'"
--Returns
--"{\"access_token\":\"83b1d5d768df4d60b427619\",\"expires_in\":\"2592000\",\"token_type\":\"Bearer\",\"refresh_token\":null}"
--Example of searching for a SKU number that found several files
set match_files to do shell script "curl --location -g 'https://example.canto.com/api/v1/search?tags=1381035&sortBy=time&sortDirection=descending&limit=100'"
--Example of downloading a found image
do shell script "curl --location -g 'https://example.canto.com/api_binary/v1/image/dkkfofh16d5iv2kr0jr3btvl66'"
I figured it out. I needed to add " --header 'Authorization: Bearer 8db9e5780acc4d8185e56781230a" to the two Shellscript lines of code. I figured the access token might need to be added somewhere but I wasn’t sure how exactly to write it until I researched it in the Postman app.
Here is the final script:
--------------------------------------------------------------------------------------------------
-- Getting the SKU number
---------------------------------------------------------------------------------------------------
set sku_number to "1234567"
---------------------------------------------------------------------------------------------------
-- Creating lists
---------------------------------------------------------------------------------------------------
--Creating an empty download URL list to add to later on
set download_url_list to {}
---------------------------------------------------------------------------------------------------
-- Searching for files by SKU number
---------------------------------------------------------------------------------------------------
--Example of searching for a SKU number that found several files
set match_files to do shell script "curl --location -g 'https://example.canto.com/api/v1/search?tags=" & sku_number & "&sortBy=time&sortDirection=descending&limit=100' --header 'Authorization: Bearer 8db9e5780acc4d8185e56781230a'"
---------------------------------------------------------------------------------------------------
-- Adding the download links for each found image to the download list
---------------------------------------------------------------------------------------------------
--Using Applescript's text item delimiters to isolate the download links from the returned list
if match_files contains "\"download\":\"" then
--Calling split by delimiter function at bottom of script to split apart the giant list by what comes before and after the word download:
set list_parts to my delimit_every_text_item((match_files as *string*), "\"download\":\"")
--Looping through the split apart list to isolate only the download link
repeat with an_item in list_parts
if an_item contains "\",\"metadata\"" then
--Using text item delimiters again to get only the information before the word metadata
set poss_download_link to delimit_by_position(an_item, "first", "\",\"metadata\"")
--Adding the possible download link to the download url list if it appears to be a real download link
try
if *text* 1 thru 5 of poss_download_link contains "https" and poss_download_link contains "api_binary" then
set download_url_list to download_url_list & (poss_download_link & "/directuri" as *string*)
end if
end try
end if
end repeat
---------------------------------------------------------------------------------------------------
-- Downloading each image
---------------------------------------------------------------------------------------------------
--Looping through each download URL
repeat with a_download_url in download_url_list
--Getting the direct URL from Cloudfront (the URL from Canto was not downloading, this fixes that problem)
set direct_URI to do shell script "curl --location -g " & a_download_url & " --header 'Authorization: Bearer 8db9e5780acc4d8185e56781230a'"
--Isolating the filename from the URL
set part_one to delimit_by_position(direct_URI, "last", "filename")
set part_two to delimit_by_position(part_one, "last", "%27")
set the_filename to delimit_by_position(part_two, "first", "&Expires")
--Downloading the image
do shell script "curl " & "'" & direct_URI & "'" & " > ~/Downloads/" & the_filename
end repeat
end if -->end if match_files contain download
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--FUNCTIONS
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--Function to use text item delimiters to isolate a part of the result
on delimit_by_position(the_URL, the_position, the_delimiter)
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to the_delimiter
if the_position contains "first" or the_position contains "beginning" then
set my_item to first *text item* of the_URL
else
set my_item to last *text item* of the_URL
end if
return my_item
end delimit_by_position
---------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------
--Function to use text item delimeters to split items into separate lists
on delimit_every_text_item(this_text, search_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every *text item* of this_text
set AppleScript's text item delimiters to ""
return item_list
end delimit_every_text_item
1 Like