通过ruby脚本执行Google Apps脚本(`check_status':notFound:找不到请求的实体。(Google :: Apis :: ClientError)

时间:2019-04-22 15:09:10

标签: ruby google-apps-script google-apps-script-api

我有一个Google Apps脚本,该脚本运行一个宏来在我的Google表格上执行操作。我希望我的ruby脚本执行Google工作表中的宏。

我在尝试执行时遇到身份验证问题。剧本。我在下面使用了ruby快速入门。

https://developers.google.com/apps-script/api/quickstart/ruby

一切正常,但是它打开了品牌应用程序,因为那就是脚本的作用。我对其进行了修改,以针对显示在Google网页的同一列表中的其他脚本。我已经在下面尝试过了

https://developers.google.com/apps-script/api/how-tos/execute

每次运行它,我都会收到此错误 `check_status': notFound: Requested entity was not found. (Google::Apis::ClientError)

这是我的代码

require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'KPI Macros'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = 'https://www.googleapis.com/auth/spreadsheets.currentonly'.freeze

# Ensure valid credentials, either by restoring from the saved credentials
# files or intitiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
def authorize
  client_id = Google::Auth::ClientId.from_file(CREDENTIALS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  print(credentials)
  if credentials.nil?
    url = authorizer.get_authorization_url(base_url: OOB_URI)
    puts 'Open the following URL in the browser and enter the ' \
         "resulting code after authorization:\n" + url
    code = gets
    credentials = authorizer.get_and_store_credentials_from_code(
      user_id: user_id, code: code, base_url: OOB_URI
    )
  end
  credentials
end

# Initialize the API
service = Google::Apis::ScriptV1::ScriptService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
script_id = 'MY SCRIPT ID HERE'

# Make the API request.
request = Google::Apis::ScriptV1::ExecutionRequest.new(
  function: 'failedTest'
)

response = service.run_script(script_id, request)
print(response)

任何调试帮助将不胜感激。

0 个答案:

没有答案