'How to upload file in Google Drive fetched from a 3rd party API using NodeJS and Google Drive API

I am fetching a file from a third-party API and it is returning me a base64 string of data as a response. Now when I'm trying to upload files to Drive using Drive API and that base 64 string it's working but not opening the files in the Drive except text file.

const drive = google.drive({
    version: "v3",
    auth: oauth2Client,
  });
  var fileMetadata = {
    name: fileToUpload.name,
    mimeType: fileToUpload.mimeType,
    parents: [fileToUpload.parentFolderId],
  };
  drive.files.create(
    {
      resource: fileMetadata,
      media: {
        body: fileToUpload.data, //the base64 string got from the response.
        mimeType: fileToUpload.mimeType,
      },
      fields: "id",
    },
    function (err, file) {
      console.log(err);
    }
  );

When opening the file in Drive it's not opening. enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source