Ala*_*lls 9 google-apps-script google-drive-api
我需要使用 Google Apps Script Advanced Drive Service获取文本文件的文件内容。此问题特定于 Google Apps 脚本。
可以通过使用 Drive API 发出 HTTPS 请求来获取文件内容,但我不想发出 HTTPS 请求。因此,我并不是要求“解决方法”或不使用高级驱动服务的方法。
可以使用文件的自链接和“alt=media”选项,通过对 Drive REST API 的 HTTPS 请求来获取文件内容。但我不想这样做,因为我想避免用户授权外部请求。
这是我现在使用 Drive REST API 执行此操作的方式。
function getContentsOfTxtFile_(po) {
try{
var i,options,rtrnObj,selfLink,tkn,url;
/*
PASSED IN PARAMETERS
po.id - the file id of the text file to get
*/
selfLink = 'https://www.googleapis.com/drive/v3/files/' + po.id;
url = selfLink + '?alt=media';//using alt=media returns the file content instead of the metadata resource
tkn = ScriptApp.getOAuthToken();//Get the OAuth token
options = {};
options.headers = {Authorization: 'Bearer ' + tkn}
options.muteHttpExceptions = true;
for (i=1;i<3;i++) {//Only loop twice because sometimes there will legitimately not be a file
try{
rtrnObj = UrlFetchApp.fetch(url,options);//Make an external request to get the file content
break;//If successful break out of the loop
} catch(e) {
if (i!==2) {Utilities.sleep(i*1500);}
if (i>=2) {
console.log('ERROR getting file content: ' + e + "Stack: " + e.stack)
}
};
}
if (!rtrnObj) {
return false;
}
if (rtrnObj.getResponseCode() !== 200) {
return false;
}
return rtrnObj.getContentText();
}catch(e){
console.error('Error ' + e)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试过的:
function getFileContent() {
try{
var content = Drive.Files.get('File ID Here',{alt:'media'});
}catch(e) {
Logger.log('message: ' + e.message)
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码会产生错误:
消息:响应代码:200。消息:文件内容在这里
还可以从文件元数据中获取“Web 内容链接”,然后使用 webContentLink 来:
引用:
用于在浏览器中下载文件内容的链接。这仅适用于 Google 云端硬盘中包含二进制内容的文件
文档位于:https://developers.google.com/drive/api/v3/reference/files
我努力了:
function getFileContentW_Export() {
try{
var content = Drive.Files.export('FileID?alt=media','text/plain');
}catch(e) {
Logger.log('message: ' + e.message)
}
Logger.log('content: ' + content)
}
Run Code Online (Sandbox Code Playgroud)
或者:
var content = Drive.Files.export('FileID','text/plain');
Run Code Online (Sandbox Code Playgroud)
Advanced Drive API 似乎无法获取文件内容。尽管 REST API 能够获取文件内容,但 Advanced Drive API 似乎故意省略了此功能。
我在问题跟踪器中发布了错误报告,但缺乏此功能并不被视为错误。
https://issuetracker.google.com/issues/149104685
DriveApp
如果我可以将它与作用域一起使用,"https://www.googleapis.com/auth/drive.file"
但DriveApp.getFileById('ID')
不能与该作用域一起使用,我很乐意使用。下面的代码:
var content = DriveApp.getFileById('ID').getAs(MimeType.PLAIN_TEXT).getDataAsString();
Run Code Online (Sandbox Code Playgroud)
返回错误:
例外:您无权调用 DriveApp.getFileById。所需权限:( https://www.googleapis.com/auth/drive.readonly || https://www.googleapis.com/auth/drive )
甚至“只读”范围也https://www.googleapis.com/auth/drive.readonly
受到限制。请参阅:链接部分中的“Gmail 和 Drive API”:https://support.google.com/cloud/answer/9110914#restricted-scopes
https://developers.google.com/apps-script/reference/drive/drive-app?hl=en#getFileById(String)
Jef*_*ush -1
我测试了不同的范围:drive和drive.file forFiles.export()
和Files.get()
只有drive.file范围给了我404
为什么?
因为drive.file用于应用程序创建或打开的文件
在这里检查:验证您的用户
function fileExport(auth) {
const drive = google.drive({ version: 'v3', auth });
drive.files.export({
fileId: fileId,
mimeType: "text/plain"
}).then(res => console.log(res))
.catch(e => console.log(e));
}
Run Code Online (Sandbox Code Playgroud)
对于在驱动器中创建的文件,请使用:
function fileGet(auth) {
const drive = google.drive({ version: 'v3', auth });
drive.files.get({
fileId: fileId
}).then(res => console.log(res))
.catch(e => console.log(e));
}
Run Code Online (Sandbox Code Playgroud)