javascript - Uploading a File to Google Drive : Insufficient Permission
问题描述
I am trying to upload the image file green.png to my google drive but continue to get the error the message
"INSUFFICIENT PERMISSION".
I suspect it has to do with my SCOPES
. I have modified my scopes many times and continue to get the error.
Does anyone have any ideas what I could do to my code to successfully upload it to my drive? Thank you
var path = require("path");
const fs = require('fs');
const readline = require('readline');
const {google} = require('googleapis');
const OAuth2Client = google.auth.OAuth2;
const TOKEN_PATH = 'credentials.json';
var fileArr=[];
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
var multer = require("multer");
var upload = multer({dest: "./uploads"});
const SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.metadata',
'https://www.googleapis.com/auth/drive.photos.readonly',
'https://www.googleapis.com/auth/drive.appdata',
];
module.exports=function(app){
app.post("/upload2", upload.array('file', 4) , function(req, res, next){
const TOKEN_PATH = 'credentials.json';
fs.readFile('client_secret.json', (err, content) => {
if (err) return console.log('Error loading client secret file:',
err);
authorize(JSON.parse(content), UploadFiles);
});
function authorize(credentials, callback) {
const {client_secret, client_id, redirect_uris} =
credentials.installed;
const oAuth2Client = new OAuth2Client(client_id, client_secret,
redirect_uris[0]);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}
function UploadFiles(auth) {
console.log(auth)
const drive = google.drive({version: 'v3', auth});
var folderId = '1AM07eNAJz5D4oFa5ZB-0ygOa_tVjoAgR';
var fileMetadata = {
'name': 'green.png'
parents: [folderId]
};
var media = {
mimeType: 'image/png',
body: fs.createReadStream('./img/green.png')
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file) {
if (err) {
// Handle error
console.log(err);
} else {
console.log('File Id: ', file.id);
}
});
}
})
}
解决方案
代码:403,错误:[{域:'global',原因:'insufficientPermissions',消息:'Insufficient Permission'}]}
确切地说,您已通过身份验证的用户无权执行您尝试执行的操作。
在您的情况下,两件事之一可能是您的问题的原因。
您已通过身份验证的用户无权访问下面的文件夹,您可能错过了键入它可能属于另一个用户并且您忘记与该用户共享。
var folderId = '1AM07eNAJz5D4oFa5ZB-0ygOa_tVjoAgR';
第二种选择是您目前正在包括您真正不需要的所有范围。但是,如果当您第一次验证此用户时,您只有请求阅读我,那么该用户仅授予阅读我的权限。您需要对您的用户进行身份验证,它将提示您在代码中添加的其他范围的权限,然后它将起作用。
你真的只需要它就可以访问所有内容。
'https://www.googleapis.com/auth/drive',
推荐阅读
- conda - 将 Snakemake 的 conda 环境移动到另一个位置
- python - 如何根据优先级顺序替换数据框列?
- javascript - 带有机车平滑滚动库的浏览器原生滚动条不是自定义的
- c# - 将字符串数组拆分为组
- ruby-on-rails - 自我速记的行为方式与 class << self rails 不同
- php - 按自定义列 WooCommerce 中的可点击值过滤产品
- reactjs - 带有 getStaticProps 和 typescript 的空对象
- c++ - Qt 线程事件循环不分派自创信号
- javascript - clearInterval "变量未定义" |
- json - Ingress-Nginx 外部身份验证的自定义响应