google drive api v3使用节点js中的服务帐户创建csv或电子表格

问题描述

我正在使用带有角度的节点,我需要创建一个API以使用V3 API在Google驱动器上创建电子表格文件创建文件后,我们需要授予对Google云端硬盘的访问权限,以下链接与授予权限有关: Grant permission to an existing file on google drive api v3 in node js

解决方法

const google = require("googleapis").google;
const credentials = require('path to your service account file credentials.json');

async function uploadCsvFile(filename,content,callback) {
    const client = await google.auth.getClient({
        credentials,scopes: ["YOUR SCOPES"]
    });
    const drive = google.drive({ version: 'v3',auth: client });
    drive.files.create({
        resource: {
            name: filename,mimeType: 'application/vnd.google-apps.spreadsheet',convert: true,},media: {
            mimeType: 'text/csv',body: content,fields: 'id,webViewLink'
    },function (err,res) {
        if (err) return console.log(err);
        const files = res;
        if (files) {
            callback(files.data);
        } else {
            console.log('No files found.');
        }
    });

}