Hello,
The downloading of a report is in 2 steps (i.e. 2 HTTP requests).
The first request to the reportUrl must have an Authentication header with a Genesys Cloud bearer/access token.
If successful, you will get a 303 HTTP Response (redirect) with the url to S3 to use to download the file (as Location header of the 303 HTTP Response).
The second request (to S3) must not have the Authentication header defined.
Note that you must use the same user/server context than the one used to generate the report.
I mean that you need to use a Genesys Cloud access token related to the same user/server context.
Assuming you used 'request' npm to make your HTTP requests, here is an example (not optimized - you may have to clean this code a bit):
let targetDirectory = 'D:/';
let fileName = name;
let ext = 'csv';
var sourceURL = downloadUrl;
var accessToken = genesysAccessToken;
request.get({
url: sourceURL,
headers: {
'Authorization': 'Bearer ' + accessToken
}
}, function (error, response, body) {
// In case of 303 Redirect, the url to download the file is available in the HTTP Response Location header
// You can access it using response.request.href
console.log('URL to access the file: ', response.request.href);
let file = fs.createWriteStream(targetDirectory + fileName + '.' + ext);
let stream = request({
uri: response.request.href,
headers: {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'en-US,en;q=0.9,fr;q=0.8,ro;q=0.7,ru;q=0.6,la;q=0.5,pt;q=0.4,de;q=0.3',
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
'Upgrade-Insecure-Requests': '1',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
},
gzip: false
})
.pipe(file)
.on('finish', () => {
file.close();
console.log(`The file has been downloaded.`);
})
.on('error', (error) => {
console.log(`Something wrong happened: ${error}`);
})
});
Regards,