Skip to main content

I am attempting to download a file that has been posted to a Teams room. I receive the contentUri from a webhook notification and when I call platform.get(contentUri) to retrieve the file, I receive "503 Service Unavailable." The URI looks correct and I've done similar things with call recordings and voicemails. I am running my application on a production instance.

What environment and programming language do you use? Here is how I download an attachment using Node JS

// in production env
var contentUri = 'https://dl.mvp.ringcentral.com/file/xxxxxxxxxx'
var arr = contentUri.split("//")
var index = arr[1].indexOf('/')
var domain = arr[1].substring(0, index)
var path = arr[1].substring(index, arr[1].length)
downloadFile(domain, path)

var https = require('https');
async function downloadFile(domain, path){
var fileName = 'test_download.json'
var tokenObj = await platform.auth().data()
var accessToken = tokenObj.access_token
download(domain, path, accessToken, fileName, function(){
console.log("Save atttachment to the local machine.")
})
}

const download = function(domain, path, accessToken, dest, cb) {
var file = fs.createWriteStream(dest);
var options = {
host: domain,
path: path,
method: "GET",
headers: {
Authorization: `Bearer ${accessToken}`
}
}
const req = https.request(options, res =>
res.on('data', function(chunk) {
var textChunk = chunk.toString('utf8');
console.log( textChunk.toString('utf-8'))
});
})
req.on('error', error => {
console.error(error)
})
req.end()
}

I am using node.js and do the following:

Create RingCentral platform variable (from Server, Client Id, Client Secret)

Perform platform.login (with Username, Password, Extension)

Subscribe to glip notifications

Receive webhook about new Team file

From attachments array, I retrieve attachments[i].contentUri. For example: https://dl.mvp.ringcentral.com/file/16024460001xxxx

I then invoke platform.get(contentUri) for each contentUri


This is where I receive the 503. I do something very similar to retrieve voicemails and call recordings. Are Teams files somehow different?



That is incorrect. The platform.get(contentUri) is for getting an attachment metadata object.

To download the attachment, you need to implement as I showed you above


Thanks.

Sorry for all the clutter, but I am still not there. I verified that I have the correct URI which is then used to create domain and path. I am also getting an access token. However, when I call https.request there is a several second delay before I receive the following error. Also, I never seen the callback (cb) invoked.


Error: socket hang up

at createHangUpError (_http_client.js:332:15)

at TLSSocket.socketOnEnd (_http_client.js:435:23)

at TLSSocket.emit (events.js:203:15)

at endReadableNT (_stream_readable.js:1145:12)

at process._tickCallback (internal/process/next_tick.js:63:19)

Emitted 'error' event at:

at TLSSocket.socketOnEnd (_http_client.js:435:9)

at TLSSocket.emit (events.js:203:15)

at endReadableNT (_stream_readable.js:1145:12)

at process._tickCallback (internal/process/next_tick.js:63:19)



I am not sure if this is progress or not, but I changed the options object to:


var options = {

host: domain,

path: path,

method: "GET",

key: fs.readFileSync(KEY_FILE).toString(),

cert: fs.readFileSync(CHAIN_FILE).toString(),

headers: {

Authorization: `Bearer ${accessToken}`

}

}


Where KEY_FILE and CHAIN_FILE are my certificates. host is "dl.mvp.ringcentral.com" and path is

"/file/1604343431178".


The function will now execute, but instead of receiving the file contents, I get:


{"errors":[{"errorCode":"CMN-102","message":"Resource for parameter [fileId] is not found.","parameterName":"fileId"}]}


This is exactly what I get when I tried to download the file with Chrome.




Sorry Andrew, I have been super busy these days.

Just try this from the browser

https://dl.mvp.ringcentral.com/file/1603584106XXX?access_token=[Your-valid access token]


Reply