使用Google App Script而非Google Drive API存储应用程序特定的数据 [英] Store Application specific Data using Google App Script not Google Drive API

查看:124
本文介绍了使用Google App Script而非Google Drive API存储应用程序特定的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目的:如果您阅读以下链接的第一段:

Purpose: If you read the first paragraph of the following link: https://developers.google.com/drive/api/v3/appdata Now as you can see there is no option to do this using AppScript. I want to try creating an Application Data Folder using Google Sheets editor.

这可能吗?如果没有,我想念什么吗?我的逻辑错了吗?

Is this possible? If not, am I missing something? Is my logic wrong?

推荐答案

1.通过启用高级驱动器服务,您可以在Apps脚本中使用任何Drive API方法.

1. You can use any Drive API method in Apps Script by enabling the Advanced Drive Service

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

2.您可以执行网址提取请求

2. You can perform an Url fetch request

因此语法是 UrlFetchApp.fetch(url, options);

以及url和选项可以从方法描述.

and the url and options can be deduced from the method description.

更新

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

A sample how to create a file in the application folder with url fetch: Code.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())   
}

为使此代码正常工作,您需要编辑清单并提供必要的范围.

For this code to work correctly, you need to edit the manifest and provide the necessary scopes.

manifest.js:

manifest.js:

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

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

Keep in mind that the file you create will not be accessible outside of the App - this is intended behavior.

这篇关于使用Google App Script而非Google Drive API存储应用程序特定的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆