简体   繁体   English

如何使用Apps Script获取当前登录用户的Google文档列表?

[英]How do I get a list of Google Docs for the current logged in user with Apps Script?

I am working with Apps Script on a Google Site and I am trying to use Oauth to authenticate the gadget as the active user to show the active users documents list. 我正在Google网站上使用Apps Script,并且尝试使用Oauth将小工具验证为活动用户,以显示活动用户文档列表。 I have found several Google group discussions asking about this with no answers, was hoping I could get an answer on here. 我发现一些Google小组讨论都在询问有关此问题的答案,但都没有答案,希望我能在这里找到答案。 Here is my code: 这是我的代码:

var oauthConfig = UrlFetchApp.addOAuthService("gDocs"); var oauthConfig = UrlFetchApp.addOAuthService(“ gDocs”); oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken"); oauthConfig.setAccessTokenUrl(“ https://www.google.com/accounts/OAuthGetAccessToken”); oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feeds/"); oauthConfig.setRequestTokenUrl(“ https://www.google.com/accounts/OAuthGetRequestToken?scope=https://docs.google.com/feeds/”); oauthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken"); oauthConfig.setAuthorizationUrl(“ https://www.google.com/accounts/OAuthAuthorizeToken”); oauthConfig.setConsumerKey("myDomainName"); oauthConfig.setConsumerKey(“ myDomainName”); oauthConfig.setConsumerSecret("myCosumerSeceret"); oauthConfig.setConsumerSecret(“ myCosumerSeceret”);

var options = { "method": "GET", "headers": { "GData-Version": "3.0" }, "oAuthServiceName" : "gDocs", "oAuthUseToken" : "always" }; var options = {“方法”:“ GET”,“标题”:{“ GData-Version”:“ 3.0”},“ oAuthServiceName”:“ gDocs”,“ oAuthUseToken”:“始终”};

var results = UrlFetchApp.fetch("https://docs.google.com/feeds/default", options); var results = UrlFetchApp.fetch(“ https://docs.google.com/feeds/default”,选项);

At this point the code does not run and the page with the gadget displays: Authorization is required to perform that action. 此时,代码无法运行,并且显示带有小工具的页面:必须执行授权才能执行该操作。

Any assistance would be greatly appreciated. 任何帮助将不胜感激。

Thank you, James Krimm 谢谢James Krimm

In order to perform authorization using 3-legged OAuth, you have to use 'anonymous' as ConsumerKey and ConsumerSecret: 为了使用三足式OAuth执行授权,您必须使用“匿名”作为ConsumerKey和ConsumerSecret:

 oauthConfig.setConsumerKey("anonymous");
 oauthConfig.setConsumerSecret("anonymous");

Also, please note that the correct feed url is https://docs.google.com/feeds/default/private/full . 另外,请注意,正确的Feed网址是https://docs.google.com/feeds/default/private/full

However, if your goal is to get the list of documents for the active user, why don't you just use the DocsList Services provided by Apps Script? 但是,如果您的目标是获取活动用户的文档列表,那么为什么不只使用Apps Script提供的DocsList Services? They will also take care of parsing the results for you. 他们还将为您解析结果。

https://developers.google.com/apps-script/service_docslist https://developers.google.com/apps-script/service_docslist

It's not possible to access the active user data. 无法访问active user数据。 A published Apps Script, as on a site, runs under the account of the script owner, called effective user . 在站点上,已发布的Apps脚本以称为“ effective user ”的脚本所有者的身份运行。 And, as a security concern, the script owner does not have permission to access any data of the active user . 并且,出于安全考虑,脚本所有者无权访问active user任何数据。

So, what @claudio suggests (of using builtin DocsList) is not possible. 因此,@ claudio所建议的(使用内置DocsList)是不可能的。

Unless we're talking about a Google Apps domain (and not regular consumer accounts) and the script owner is the domain administrator. 除非我们谈论的是Google Apps域(而不是普通消费者帐户),否则脚本所有者是域管理员。 In which case he can use the Google Docs List Data API to impersonate any user on his domain. 在这种情况下,他可以使用Google文档列表数据API来模拟其域中的任何用户。

Either way, the consumer key and secret should always be "anonymous", regardless this scenario. 无论哪种情况,无论这种情况如何,使用者密钥和机密都应始终是“匿名的”。

I have a Google Script OAuth library https://sites.google.com/site/scriptsexamples/custom-methods/google-oauth that will make the OAuth part less painful. 我有一个Google Script OAuth库https://sites.google.com/site/scriptsexamples/custom-methods/google-oauth ,它将使OAuth的工作减轻。

And some source code for a currently being developed DriveSrevice Library that will hit the points that are missing in Google Script. 以及当前正在开发的DriveSrevice库的一些源代码,这些源代码将达到Google脚本中所缺少的要点。 https://sites.google.com/site/scriptsexamples/custom-methods/driveservice https://sites.google.com/site/scriptsexamples/custom-methods/driveservice

This particular error is probably not related to OAuth but related to adding DocsList to the app. 此特定错误可能与OAuth不相关,但与将DocsList添加到应用程序有关。

See: https://developers.google.com/apps-script/troubleshooting#common_errors 请参阅: https//developers.google.com/apps-script/troubleshooting#common_errors

Authorization is required to perform that action. 需要授权才能执行该操作。

This error indicates that the script is lacking the authorization needed to run. 此错误表明脚本缺少运行所需的授权。 When a script is run in the Script Editor or from a custom menu item an authorization dialog is presented to the user. 在脚本编辑器中或从自定义菜单项中运行脚本时,将向用户显示授权对话框。 However, when a script is run as a service, embedded with a Google Sites page, or run from a trigger the dialog cannot be presented and this error is shown. 但是,当脚本作为服务运行,嵌入到Google Sites页面或从触发器运行时,将无法显示对话框,并显示此错误。 To authorize the script, open the Script Editor and run any function. 要授权脚本,请打开脚本编辑器并运行任何功能。 To avoid this error, remember to run the script once in the Script Editor after adding new services or capabilities to your script. 为避免此错误,请记住在向脚本中添加新服务或功能后在脚本编辑器中运行一次脚本。

The answers here are not correct. 这里的答案不正确。 You CAN do what you need, but not using Oauth directly. 您可以执行所需的操作,但不能直接使用Oauth。 Instead, publish the Apps Script with the option to "run as the current user" instead of the script owner. 而是,发布带有“以当前用户身份运行”选项而不是脚本所有者的选项的Apps脚本。 Then use DocsList of DriveApp to get at the users files. 然后使用DriveApp的DocsList获取用户文件。 The key here is to publish the service to "run as the user accessing the app". 此处的关键是将服务发布为“在用户访问应用程序时运行”。

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

相关问题 如何从django-oauth-toolkit令牌获取当前登录用户? - How can I get the current logged in user from a django-oauth-toolkit token? Google Apps脚本代表用户的推文 - Google Apps Script to Get Users' Tweets on Their Behalves php zend gdata-使用oauth获取Google文档列表 - php zend gdata - Get list of google docs using oauth 如何判断用户是否*未通过Google登录(OAuth2)登录? - How can I tell if a user is *not* logged in via Google Sign-In (OAuth2)? 如何通过Google文档脚本授权和发布/更新Trello卡 - How to authorize and post/update Trello card from a Google docs script 如何上传到谷歌文档而无需输入用户名和密码? - How to upload to google docs without having to type user and password? 如何确定用户是否已从 Microsoft 其帐户注销? - How do I findout whether a user is logged out from microsoft it's account? 如何从 Google Apps 脚本授权 Google Speech-to-text? - How can I authorize Google Speech-to-text from Google Apps script? 如何将google apps脚本发布到公共云? - How to publish google apps script to cloud for public? 如何让 Figma API 与 Google App-script API 一起使用? - How do I get Figma API to work with the Google App-script API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM