6

I am trying to download images from a Slack channel by the following code, but I just get the html code of the page.

Am I doing something silly, or they have a trick to make this difficult?

<?php

copy('https://files.slack.com/files-tmb/T1Q3K1TFB-F6TQMM6AG-baba978dff/image003_480.jpg', 'file.jpeg');

echo '<img src="file.jpeg">';
echo '<hr>';

//Get the file
$content = file_get_contents("https://files.slack.com/files-tmb/T1Q3K1TFB-F6TQMM6AG-baba978dff/image003_480.jpg");

//Store in the filesystem.
$fp = fopen("image.jpg", "w");
fwrite($fp, $content);
fclose($fp);
echo '<img src="image.jpg">';
echo '<hr>';

$url_to_image
 = 'https://files.slack.com/files-tmb/T1Q3K1TFB-F6TQMM6AG-baba978dff/image003_480.jpg';

$ch = curl_init($url_to_image);

$my_save_dir = 'images/';
$filename = basename($url_to_image);
$complete_save_loc = $my_save_dir . $filename;

$fp = fopen($complete_save_loc, 'wb');

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo '<img src="'.$complete_save_loc.'">';
echo '<hr>';

echo '<hr><img src="https://files.slack.com/files-tmb/T1Q3K1TFB-F6TQMM6AG-baba978dff/image003_480.jpg">';
echo '<hr><img src="https://files.slack.com/files-tmb/T1Q3K1TFB-F6TQMM6AG-baba978dff/image003_360.jpg">';
echo '<hr><img src="https://conversazioniconmario.slack.com/files/U1Q37UCNL/F6TQMM6AG/image003.jpg">';

?>

wget gets fooled too;

wget -nd -r -P /myLocalPath/images  -A jpeg,jpg,bmp,gif,png https://files.slack.com/files-tmb/T1Q3K1TFB-F6TQMM6AG-baba978dff/image003_480.jpg
Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
mario
  • 622
  • 1
  • 11
  • 31

3 Answers3

11

You can also download file by providing right headers:

 wget -d --header="Authorization: Bearer A_VALID_TOKEN" https://files.slack.com/files-pri/T1Q3K1TFB-F6TQMM6AG-baba978dff/download/image003.jpg
Deylo
  • 126
  • 2
  • 2
    Important to keep in mind: *In this case, A_VALID_TOKEN is representative of a real OAuth token, **bearing at least the files:read scope***. – janh Mar 21 '18 at 10:29
  • I just realized I forgot to accept your answer. It was indeed a good help. Thanks a lot. If anybody else is interested, check [here](https://api.slack.com/legacy/custom-integrations/legacy-tokens) too – mario Apr 25 '20 at 13:47
2

You can download images and other files from Slack, but not directly. You need to first mark the file as public to get its public URL.

This can be done with the API method files.sharedPublicURL. It will return a file object including the permalink_public property, which is the URL you can use to download it with your app.

After you downloaded it you can revoke the public URL again with files.revokePublicURL.

Erik Kalkoken
  • 30,467
  • 8
  • 79
  • 114
0

For people interested in Python implementation, I suggest to check this repo. I have just created a pull request so the script works with the latest Slack API. https://github.com/auino/slack-downloader

Paloha
  • 558
  • 6
  • 14