question

Andrew Prokop avatar image
Andrew Prokop asked Phong Vu commented

503 Service Unavailable when downloading files

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.

ringcentral apperrors
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered

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()
}
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Andrew Prokop avatar image
Andrew Prokop answered Andrew Prokop commented

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?


5 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

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

0 Likes 0 ·

When I call "var tokenObj = await platform.auth().data()," tokenObj.access_tok is undefined. I am using a password-based auth flow for my service.

0 Likes 0 ·

There is a mistake when I pasted the code. It needs to be

tokenObj.access_token

0 Likes 0 ·

For more convenience, the javascript sdk comes with a signUrl function available on the SDK object. This will attach the access token to the URL you pass to it (Make sure you have logged in to the SDK before doing this)

Have you tried generating the URL using

const signedUrl = rcsdk.signUrl('https://dl.mvp.ringcentral.com/file/1603584106XXX');

and then use the signedUrl to make an HTTP get request to get the resource

0 Likes 0 ·
Andrew Prokop avatar image Andrew Prokop Yatin Gera ♦♦ ·

@Yatin Gera I tried that, but it didn't get me anywhere. I just posted a new message to the thread where I am getting closer.

0 Likes 0 ·
Andrew Prokop avatar image
Andrew Prokop answered Andrew Prokop commented

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)


2 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Can you download the attachment by opening the contentUri with the access token using a browser?

0 Likes 0 ·

Is this what you want?

I used the ModHeader Chrome Extension to set the Authorization header to "Bearer xxxxxx". I then entered the URI into Chrome (https://dl.mvp.ringcentral.com/file/1603584106XXX). I press enter and see this:


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

In the developer tools / Network I see this under General:


  1. Request URL:
  2. Request Method:
    GET
  3. Status Code:
    404
  4. Remote Address:
    127.0.0.1:9000
  5. Referrer Policy:
    strict-origin-when-cross-origin
0 Likes 0 ·
Andrew Prokop avatar image
Andrew Prokop answered Phong Vu commented

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.



5 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

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]

0 Likes 0 ·

No apology is necessary. I know all about super busy.

I tried that URL in Chrome and received the same result. I used the access token from platform.auth().data().


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

0 Likes 0 ·

So it seems the file id is invalid. I remember that I got this some time too. Can you double check by upload a new file then use the new file id with one of the methods to download it.

Let me know

0 Likes 0 ·
Show more comments

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys