【问题标题】:Store Application specific Data using Google App Script not Google Drive API使用 Google App Script 而非 Google Drive API 存储应用程序特定数据
【发布时间】:2020-05-19 06:05:11
【问题描述】:

目的:如果您阅读了以下链接的第一段:https://developers.google.com/drive/api/v3/appdata 现在正如您所见,没有使用 AppScript 执行此操作的选项。我想尝试使用 Google 表格编辑器创建应用程序数据文件夹。

这可能吗?如果没有,我是否错过了什么?我的逻辑错了吗?

【问题讨论】:

  • 可以使用高级 Google 服务或 urlfetch 从应用程序脚本访问驱动器 api。有关详细信息,请参阅tag info page。

标签: google-apps-script google-sheets-api


【解决方案1】:

1.通过启用Advanced Drive Service

,您可以在 Apps 脚本中使用任何 Drive API 方法

语法是 Drive.Files.insert(resource, mediaData, optionalArgs)

2。您可以执行Url fetch 请求

其中的语法是 UrlFetchApp.fetch(url, options);

并且可以从method description推导出url和选项。

更新

如何使用 url fetch 在应用程序文件夹中创建文件的示例: 代码.gs

function uploadToSharedDrive(){
  var url = "https://www.googleapis.com/upload/drive/v3/files?supportsTeamDrives=true&uploadType=resumable"; 
  var blob = Utilities.newBlob('This is the content!'); 
  var metaData = {
    'name' :'config.json',
    'parents' :["appDataFolder"]
  }
  params = {
    headers: {
      Authorization: 'Bearer ' + ScriptApp.getOAuthToken()   
    },
    contentType:  'application/json',    
    method: 'post',
    payload: JSON.stringify(metaData),
  }
  var response = UrlFetchApp.fetch(url, params);
  var data = blob.getBytes();
  var params2 = {
    method: "put",
    payload: data,
    muteHttpExceptions: true,
  };
  location = response.getHeaders().Location;
  var response = UrlFetchApp.fetch(location, params2);
  Logger.log(response.getContentText())   
}

要使此代码正常工作,您需要edit the manifest 并提供必要的scopes。

manifest.js:

{
...
   "oauthScopes": ["https://www.googleapis.com/auth/drive.appdata", "https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request"]
...
}

请记住,您创建的文件将无法在应用程序之外访问 - 这是预期行为。

【讨论】:

  • 我真的不明白上面解释了什么,我是 appscript 的新手,我对上面的解释感到困惑。 @ziganotschka
  • 我假设您知道如何使用 Drive API?所以我试图解释的是,您可以从 Apps 脚本中调用 Drive API。它只是被称为 Drive 而不是 Drive API,但它为您提供了与 Drive API. Please refer to the linked documentation to see how to enable and use Drive 相同的方法。如果您在此之后仍有疑问,请具体说明不清楚的地方。
  • 启用 Drive API 不是问题,我发现很难创建对用户隐藏的应用程序数据文件夹。基本上,当用户安装应用程序时,会在该应用程序内创建一个文件夹用户无法访问或可见。我想创建一个只能由开发人员访问的文件夹。
  • 您能否在没有 Apps 脚本的情况下使用 Drive API 成功执行此请求?
  • 不,我没有在没有 appscript 的情况下执行此请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多