简体   繁体   中英

How do I get the direct link to an attached file from Google's Calendar API?

This is part of the data I receive from Google's Calendar API:

{  
    "fileUrl":"https://drive.google.com/file/d/1q-cSkADjUnD0PGOamZKI44oIL_cXDJlg/view?usp=drive_web",
    "title":"2.png",
    "iconLink":"",
    "fileId":"1q-cSkADjUnD0PGOamZKI44oIL_cXDJlg"
}

How do I achieve the direct link to the file (using JavaScript) so I can use it as datasource? example:

https://something.com/2.png

Edit: This link (right click & copy image adress) works as datasource, I just don't know how I would dynamically create it.

You need to make a GET request to fileUrl , since permanent "direct" links will not exist.

Directly from these google docs :

Using alt=media To download files, you make an authorized HTTP GET request to the file's resource URL and include the query parameter alt=media. For example:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media

URLs do not need file extensions for their server to understand what it is you are requesting. However, sometimes servers want an extra query parameter to decide what to send back.

So it also seems you may need to append ?alt=media onto the end of your endpoint url, fileUrl .

Then just use ajax, or a similar requesting method, and send a GET request to the endpoint.

Using the Drive API and JS:

var fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
var dest = fs.createWriteStream('/tmp/photo.jpg');
drive.files.get({
  fileId: fileId,
  alt: 'media'
})
    .on('end', function () {
      console.log('Done');
    })
    .on('error', function (err) {
      console.log('Error during download', err);
    })
    .pipe(dest);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM