Google Apps脚本执行API服务授权每小时失败一次

科特·格罗贝拉尔

我大约每1.5分钟使用C#应用程序执行一次Google Apps脚本。apps脚本在电子表格之间移动内容,并编辑工作表。我也使用Drive API。

我的脚本可以长期正常运行,但事实是我每小时会出现5分钟的授权错误。

这是我处理授权的代码:

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;

        }

    }
}

我得到这样的服务:

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

但是大约一小时后,apps脚本调用将引发以下错误:

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

需要明确的是,它在所有其他时间都起作用,而并非在小时结束时的那5分钟内起作用。我确实在开发人员控制台和应用程序脚本编辑器中的资源>高级Google服务...下都激活了Google Drive API。

那么发生了什么?这个可以解决吗?

科特·格罗贝拉尔

经过进一步研究,我发现只需从脚本编辑器运行脚本即可防止此错误。这是我发现的信息:

需要授权才能执行该操作。

此错误表明脚本缺少运行所需的授权。在脚本编辑器中或从自定义菜单项中运行脚本时,将向用户显示授权对话框。但是,当脚本作为服务运行,嵌入到Google Sites页面或从触发器运行时,将无法显示对话框,并显示此错误。要授权脚本,请打开脚本编辑器并运行任何功能。为避免此错误,请记住在向脚本添加新的服务或功能之后,在脚本编辑器中运行一次脚本。

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

我对代码进行的另一个相关改进是在创建两个服务中的每一个之前获取凭据对象的新副本:换句话说,我对此进行了更改:

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);

这本身并不能解决我的问题,但有助于避免OAuth怪癖,并可以防止不必要的令牌刷新。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

是否可以在Google Apps中设置时钟触发器以每小时发送一次电子表格?

为什么我的Google Cloud SQL实例每小时收费一次?

在Google Apps脚本中提取多个api请求,一次解析

Google Apps脚本仅经过一次迭代就停止了

使用服务帐户从PHP调用Google Apps脚本执行API

每小时每小时执行一次方法

Google Apps脚本错误-执行此操作需要授权

Google Apps脚本的授权错误

通过Google Apps脚本进行Spotify API授权

如何每 N 分钟执行一次 Google Cloud Run 服务?

计划Python脚本每小时准确运行一次

Google Cloud API服务授权失败-禁止访问

每小时随机执行一次Cron作业

每小时执行一次SQL Counter Select语句

每小时自动执行一次屏幕中断

google脚本一次标记第一行

Google Apps 脚本动态更新授权范围

执行Google Apps网域授权

使用Google Apps脚本API可执行服务器到服务器的身份验证

Google表格脚本:一次使用两个

ClockTriggerBuilder类:特定日期和时间(仅一次)Google表格脚本中的触发器使每分钟重复执行一次

PostgreSQL:备份脚本仅创建一次备份,而不是每小时一次

如何从Google Apps脚本授权Google语音转文本?

Google GPS 即服务仅工作一次

Google 表单审批工作流程 | Apps 脚本中的问题:“执行该操作需要授权。”

每小时积分一次(行数)

每周每小时一次的Cronjob

使用Google的Text to Speech API一次执行多个请求时,仅获取最后一个请求的音频

API:授权失败Google Maps(黑屏)