简体   繁体   中英

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. I have found several Google group discussions asking about this with no answers, was hoping I could get an answer on here. Here is my code:

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

var options = { "method": "GET", "headers": { "GData-Version": "3.0" }, "oAuthServiceName" : "gDocs", "oAuthUseToken" : "always" };

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

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

In order to perform authorization using 3-legged OAuth, you have to use 'anonymous' as ConsumerKey and ConsumerSecret:

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

Also, please note that the correct feed url is 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? They will also take care of parsing the results for you.

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

It's not possible to access the active user data. A published Apps Script, as on a site, runs under the account of the script owner, called effective user . And, as a security concern, the script owner does not have permission to access any data of the active user .

So, what @claudio suggests (of using builtin DocsList) is not possible.

Unless we're talking about a Google Apps domain (and not regular consumer accounts) and the script owner is the domain administrator. In which case he can use the Google Docs List Data API to impersonate any user on his domain.

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.

And some source code for a currently being developed DriveSrevice Library that will hit the points that are missing in Google Script. 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.

See: 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. 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. Instead, publish the Apps Script with the option to "run as the current user" instead of the script owner. Then use DocsList of DriveApp to get at the users files. The key here is to publish the service to "run as the user accessing the app".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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