Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script &引用;执行该行动需要获得授权。”;消息,即使在单击“以后”;允许;_Google Apps Script_Oauth 2.0_Google Api_Google Oauth_Google Cloud Sql - Fatal编程技术网

Google apps script &引用;执行该行动需要获得授权。”;消息,即使在单击“以后”;允许;

Google apps script &引用;执行该行动需要获得授权。”;消息,即使在单击“以后”;允许;,google-apps-script,oauth-2.0,google-api,google-oauth,google-cloud-sql,Google Apps Script,Oauth 2.0,Google Api,Google Oauth,Google Cloud Sql,我最近遇到了一个问题,授权了一个新的谷歌应用程序脚本项目,特别是一个使用云SQL管理API的项目 同样的代码存在于先前授权的天然气项目中,可以正常工作,但如果我复制了天然气项目并尝试第一次运行功能,我将无法完成授权流程。下面列出了我要浏览的屏幕: 需要授权。-单击“查看权限” 选择一个帐户来授权谷歌项目。-单击我的帐户 此应用未经验证!-单击“转到项目” (不安全) Google project希望访问此范围列表。-单击“允许” 执行该行动需要授权 警告屏幕(3)是该过程的最新添加。我不记得在今

我最近遇到了一个问题,授权了一个新的谷歌应用程序脚本项目,特别是一个使用云SQL管理API的项目

同样的代码存在于先前授权的天然气项目中,可以正常工作,但如果我复制了天然气项目并尝试第一次运行功能,我将无法完成授权流程。下面列出了我要浏览的屏幕:

  • 需要授权。-单击“查看权限”
  • 选择一个帐户来授权谷歌项目。-单击我的帐户
  • 此应用未经验证!-单击“转到项目” (不安全)
  • Google project希望访问此范围列表。-单击“允许”
  • 执行该行动需要授权
  • 警告屏幕(3)是该过程的最新添加。我不记得在今年早些时候创建和运行新项目时遇到过这种情况。我想知道Google最近是否对OAuth2.0的安全实现做了任何更改

    此外,这个问题似乎只影响对云SQL管理API的REST调用。在上面提到的同一个项目中,我能够在同一个Google项目中运行将数据写入BigQuery表的函数,该项目也托管云SQL实例。显然,一些作用域和代码可以正常工作

    “”范围包含在我请求并批准的范围列表中。我甚至尝试手动编辑URL以添加更多被请求的作用域,但仍然无法通过“需要授权才能执行该操作”屏幕

    有人知道我的想法吗

    编辑:

    触发身份验证的相关代码

    // Function to get the ip address of a given CloudSQL instance
    function _getInstanceIpAddress_(projectId, sqlInstance) {
    
      var token = _getAuthenticationToken_();
    
      // Create the header authorisation  
      var headers = {
        "Authorization": "Bearer " + token
      };
    
      // Create the Cloud SQL instances get parameters
      var parameters = {
        "method": "get",    
        "headers": headers,
        "instance": sqlInstance,
        "project": projectId,
        "muteHttpExceptions": true
      };
    
      // Create the url of the sql instances get API    
      var api = "https://www.googleapis.com/sql/v1beta4/projects/" + projectId + "/instances/" + sqlInstance + "?fields=ipAddresses";   
    
      try {
        // Use the url fetch service to issue the https request and capture the response
        var response = UrlFetchApp.fetch(api, parameters);    
    
        // Extract the ip address of the instance from the response
        var content = JSON.parse(response.getContentText());
    
        return content.ipAddresses[0].ipAddress; 
      } catch(err) {
        _log_('ERROR', 'Getting ' + sqlInstance + ' instance ip address failed: ' + err);
        return null;
      }
    }
    
    function _getAuthenticationToken_() {
      // Check we have access to the service
      var service = getService();
      if (!service.hasAccess()) {
        var authorizationUrl = service.getAuthorizationUrl();
        _log_('INFO', 'Open the following URL and re-run the script: ' + authorizationUrl);
        return;
      }
    
      Logger.log('Passed Authentication');
    
      //Get the Access Token
      return service.getAccessToken();
    
      function getService() {
        // Create a new service with the given name. The name will be used when
        // persisting the authorized token, so ensure it is unique within the
        // scope of the property store.
        return OAuth2.createService('companyName-dev-service')
    
        // Set the endpoint URLs, which are the same for all Google services.
        .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')
        .setTokenUrl('https://accounts.google.com/o/oauth2/token')
    
        // Set the client ID and secret, from the Google Developers Console.
        .setClientId(CLIENT_ID)
        .setClientSecret(CLIENT_SECRET)
    
        // Set the name of the callback function in the script referenced
        // above that should be invoked to complete the OAuth flow.
        .setCallbackFunction('authCallback')
    
        // Set the property store where authorized tokens should be persisted.
        .setPropertyStore(PropertiesService.getUserProperties())
    
        // Set the scopes to request (space-separated for Google services).
        // this is admin access for the sqlservice and access to the cloud-platform:
        .setScope(
          'https://www.googleapis.com/auth/sqlservice.admin ' + 
          'https://www.googleapis.com/auth/cloud-platform')
    
        //Removed because this Should be covered by cloud-platform
        //'https://www.googleapis.com/auth/devstorage.read_write ' 
    
        // Below are Google-specific OAuth2 parameters.
    
        // Sets the login hint, which will prevent the account chooser screen
        // from being shown to users logged in with multiple accounts.
        .setParam('login_hint', Session.getActiveUser().getEmail())
    
        // Requests offline access.
        .setParam('access_type', 'offline')
    
        // Forces the approval prompt every time. This is useful for testing,
        // but not desirable in a production application.
        .setParam('approval_prompt', 'force');
      }
    
      function authCallback(request) {
        var cloudSQLService = getService();
        var isAuthorized = cloudSQLService.handleCallback(request);
    
        if (isAuthorized) {
          _log_('INFO', 'Access Approved');
          return HtmlService.createHtmlOutput('Success! You can close this tab.');
        } else {
          _log_('INFO', 'Access Denied');
          return HtmlService.createHtmlOutput('Denied. You can close this tab');
        }
      }
    }
    

    如果你回想一年前,你可能还记得你看到的是谷歌对此的反应

    使用特定作用域的Web凭据要求Google在任何人(创建相关凭据的开发人员除外)可以使用之前批准它们。谷歌表示,通常需要一周左右的时间才能获得批准

    您以前没有见过它,因为这只是最近才出现的应用程序脚本

    从2017年7月18日开始,请求某些敏感OAuth作用域的Google OAuth客户端将接受Google的审查


    谷歌计算引擎API也有类似的问题。根据为我们解决的问题,在appsscript.json文件中显式设置作用域:

    "oauthScopes": [
        "https://www.googleapis.com/auth/spreadsheets.readonly",
        "https://www.googleapis.com/auth/userinfo.email",
        "https://www.googleapis.com/auth/script.container.ui",
        "https://www.googleapis.com/auth/script.external_request",
        "https://www.googleapis.com/auth/spreadsheets",
        "https://www.googleapis.com/auth/compute",
        "https://www.googleapis.com/auth/cloud-platform"
      ],
    

    OP真正的问题是有点隐藏——在oauth流中单击“允许”会生成一个关于授权不足的错误,而不是授权项目。如果是这样,他可能没有授予访问权限。今天早些时候我用过这个,没有错误谢谢你的帮助,当网络钓鱼攻击第一次攻击时,我们注意到安全限制发生了变化,并通过加入“未查看应用的风险访问”谷歌集团解决了这个问题。看来这已经不够了。既然我是开发者,它不应该为我工作吗?我会考虑得到谷歌的批准,尽管它确实感觉到了相当重的参与度,特别是如果开发人员只是为自己运行的次要GCP任务编写小脚本,在他们自己的公司域下。如果您使用创建凭据时使用的同一帐户登录,这应该对您有效。我将在上午发布一些屏幕截图发布MCVE示例和复制说明?谢谢!这起作用了。这似乎是一个由两部分组成的范围问题。我扩展了清单中的范围,并手动将其添加到请求URL中,从而停止了“执行该操作需要授权”消息的出现。