Google Apps service script service runtime API authorization does not work once an hour

I run Google script applications with my C # application approximately every 1.5 minutes. Script applications move content between spreadsheets and edit worksheets. I also use Drive API.

My script works fine for a long time, except that I get authorization errors for 5 minutes every hour.

Here is my code that handles authorization:

class Authentication { public static ScriptService ScriptsAuthenticateOauth(UserCredential credential) { try { ScriptService service = new ScriptService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "MyApp", }); return service; } catch (Exception ex) { Console.WriteLine(DateTime.Now.ToString("HH:mm") + ": An authentication error occurred: " + ex.InnerException); return null; } } public static UserCredential getCredential(string clientId, string clientSecret, string userName) { string[] scopes = new string[] { DriveService.Scope.Drive, // view and manage your files and documents DriveService.Scope.DriveAppdata, // view and manage its own configuration data DriveService.Scope.DriveAppsReadonly, // view your drive apps DriveService.Scope.DriveFile, // view and manage files created by this app DriveService.Scope.DriveMetadataReadonly, // view metadata for files DriveService.Scope.DriveReadonly, // view files and documents on your drive DriveService.Scope.DriveScripts, // modify your app scripts ScriptService.Scope.Drive, "https://www.googleapis.com/auth/spreadsheets", "https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds"}; return GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret } , scopes , userName , CancellationToken.None , new FileDataStore("Google.Sheet.Sync.Auth.Store")).Result; } public static DriveService DriveAuthenticateOauth(UserCredential credential) { try { DriveService service = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "MyApp", }); // Console.WriteLine("Auth success"); return service; } catch (Exception ex) { // Console.WriteLine(ex.InnerException); Console.WriteLine(DateTime.Now.ToString("HH:mm") + ": An authentication error occurred: " + ex.InnerException); return null; } } } 

I get the following services:

  var credential = Authentication.getCredential(CLIENT_ID, CLIENT_SECRET, Environment.UserName); DriveService driveService = Authentication.DriveAuthenticateOauth(credential); ScriptService scriptService = Authentication.ScriptsAuthenticateOauth(credential); 

But at about the end of the hour, the script application causes the following error:

 Script error message: Authorization is required to perform that action. 

Just to be clear, it works at all other times, but not in those 5 minutes at the end of the hour. I activated the Google Drive APIs both on my developer console and in the Resources> Advanced Google Services ... section of the script editor application.

So what's going on? Could this be fixed?

+5
source share
3 answers

After further research, I found that the script just needs to be run from the script editor to prevent this error. This is the information I found:

Authorization is required to complete this action.

This error indicates that the script lacks the authorization required to run. When a script is run in a script editor or in a user menu item, an authorization dialog is provided to the user. However, when the script is launched as a service embedded on the Google Sites page or launched from a trigger, a dialog cannot be presented and this error is shown. To authorize a script, open the script editor and run any function. To avoid this error, be sure to run the script once in the script editor after adding new services or features to your script.

Source: https://developers.google.com/apps-script/troubleshooting#common_errors

Another related improvement I made with my code was to get a new copy of the credential object before creating each of the two services: In other words, I changed this:

 var credential = Authentication.getCredential(CLIENT_ID, CLIENT_SECRET, Environment.UserName); DriveService driveService = Authentication.DriveAuthenticateOauth(credential); ScriptService scriptService = Authentication.ScriptsAuthenticateOauth(credential); 

:

 var credential = Authentication.getCredential(CLIENT_ID, CLIENT_SECRET, Environment.UserName); DriveService driveService = Authentication.DriveAuthenticateOauth(credential); credential = Authentication.getCredential(CLIENT_ID, CLIENT_SECRET, Environment.UserName); ScriptService scriptService = Authentication.ScriptsAuthenticateOauth(credential); 

This in itself did not help solve my problem, but it helps to avoid OAuth bypass and can prevent unnecessary token updates.

+2
source

You probably need an update token. Access points are designed to last only 3,600 s (1 hour), usually from Google, and the update token is much longer and is designed so that you can easily get a new access token when the previous one expires. This might be a good start: API Client API Information for Token Responses

In addition, if you do not have an update token, but you have an access token, you can cancel the previous access token, and then authenticate again. The new answer will have an update token if it is called correctly "make sure you get the type of token "Bearer" ).

There is more information on using longer update tokens here .

0
source

If your application crashes "only in those 5 minutes near the end of the hour", you most likely encountered this error with the execution API .

Apparently, requests to the execution API with a token, which expires in 6 minutes, lead to errors "Authorization ...". This is apparently due to the fact that 6 minutes is the maximum run time of the script.

The workaround is to update your token in advance if you know when it will expire (which does not apply to Android). We hope that the error will be fixed soon.

I'm not sure if you really managed to fix your problem or if you found a suitable workaround, but you might want to publish this version of Script applications so that you can install it earlier.

0
source

Source: https://habr.com/ru/post/1234579/


All Articles