简体   繁体   English

将文件写入来自Google App Script服务器代码的附加组件上的Application Data Folder

[英]Writing file to Application Data Folder on Add-ons from Google App Script server code

As mentioned in this document about storing application data, I would like to make it possible from a Google App Script Add-on server code. 正如本文档中提到的关于存储应用程序数据的内容,我希望通过Google App脚本附加服务器代码实现。

I was able to create an arbitrary file into my google root drive folder this way. 我能够以这种方式在我的google根驱动器文件夹中创建一个任意文件。

  DriveApp.createFile('Test', 'Test content');

But was not able to figure out how to create it into (hidden) app data folder: 但无法弄清楚如何将其创建到(隐藏)应用程序数据文件夹中:

var dir = DriveApp.getFolderById("appDataFolder");
var file = dir.createFile('Test', 'Test content');

Receiving "Access denied" by executing it. 通过执行它来接收“拒绝访问”。

I guess I have to apply this following scope to my app but do not know how apply it on a google app script. 我想我必须将以下范围应用于我的应用程序,但不知道如何将其应用于谷歌应用程序脚本。

Scope: https://www.googleapis.com/auth/drive.appdata 范围: https//www.googleapis.com/auth/drive.appdata

Drive API is well activated. Drive API已被充分激活。

先进的谷歌服务

It would be nice if I could update these scopes (File>Project Properties menu): 如果我可以更新这些范围(文件>项目属性菜单),那将是很好的:

项目属性范围

Any help would be so appreciated. 任何帮助都会非常感激。

Users have to explicitly grant your Add-on access to the private AppData folder. 用户必须明确授予对私有AppData文件夹的加载项访问权限。 And currently that's only achievable by setting up an OAuth2 flow. 目前,这只能通过设置OAuth2流程来实现。

EDIT As of November 2017, Apps Script now supports explicit OAuth scopes. 编辑截至2017年11月,Apps脚本现在支持显式OAuth范围。 There is no longer a need to set up a custom OAuth flow, simply set the desired OAuth scope in the manifest file (as documented here ) to request access to a user's App Data Folder. 不再需要设置自定义OAuth流,只需在清单文件中设置所需的OAuth范围(如此处所述 ),以请求访问用户的App数据文件夹。


Here's a video that provides a good overview of how OAuth2 works (independent of Apps Script) 这是一个视频,可以很好地概述OAuth2的工作原理(独立于Apps脚本)

OAuth 2.0: An Overview OAuth 2.0:概述


If you're up to the task, you can review Google's OAuth2 documentation and figure out how to implement it for your use case, but you're better off leveraging the "official" OAuth2 library for Apps Script . 如果您能胜任这项任务,您可以查看Google的OAuth2文档,并了解如何针对您的用例实施该文档 ,但最好还是利用Apps脚本的“官方” OAuth2库

Here's another video that covers OAuth flows for Add-ons using said library: 这是另一个视频,它使用所述库覆盖附加组件的OAuth流:

OAuth2 flows in Add-ons OAuth2在附加组件中流动

Note: Make sure that you use the appropriate scope(s) in your flow. 注意:确保在流程中使用适当的范围。 In this case you only need to use one; 在这种情况下,您只需要使用一个; https://www.googleapis.com/auth/drive.appdata


Also, if you're looking for some code implementing an OAuth flow to access the App Data folder check out this github repo from Spencer Easton: 此外,如果您正在寻找一些实现OAuth流程来访问App Data文件夹的代码,请查看Spencer Easton的这个github仓库:

Apps-Script-Appfolder-Library 应用程序脚本,Appfolder库


The 2nd video is from Totally Unscripted , a vlog hosted by app script devs from the Google+ App Script community . 第二个视频来自Totally Unscripted ,这是一个由应用程序脚本开发人员托管的Google+应用程序脚本社区的视频 Check it out, its a great resource for App Script development. 看看它,它是App Script开发的一个很好的资源。

You've enabled Drive v2 API and use the alias appDataFolder which is for Drive v3. 您已启用Drive v2 API并使用适用于Drive v3的别名appDataFolder That may have contributed to the issue you're encountering. 这可能是您遇到的问题的原因。

If you'll be using v2, try to change the alias to appfolder . 如果您将使用v2,请尝试将别名更改为appfolder Hopefully this has been of help to you. 希望这对你有所帮助。

Happy coding! 快乐的编码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM