0

I have a google apps script that runs a macro to do things on my google sheet. I want my ruby script to execute the macro in the google sheet.

I am getting authentication issues when trying to execute. the script. I have used the ruby quick-start below.

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

and everything works fine but it opens a brand app because thats what the script does. I modified it to target my other script that shows up in the same list on the google webpage. I have already tried this below

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

Every-time I run it, I get this error `check_status': notFound: Requested entity was not found. (Google::Apis::ClientError)

Here is my code

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)

Any debugging help would be appreciated.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
plgelso
  • 107
  • 2
  • 15
  • Possible duplicate of [404 error when deploying a Google Apps script via the REST API](https://stackoverflow.com/questions/55453933/404-error-when-deploying-a-google-apps-script-via-the-rest-api) – TheMaster Apr 22 '19 at 16:36
  • @TheMaster Im not deploying a new version I just want to execute one of the functions in my apps script. I can deploy a new one fine using the link you provided in any event – plgelso Apr 22 '19 at 18:35
  • Try saving a version setting `devMode:true`. – TheMaster Apr 22 '19 at 19:17
  • Although I'm not sure whether this is the same situation with me, in my experience, if the error of ``Requested entity was not found.`` occurs when run the script for requesting the scripts.run method of Apps Script API after deployed API executable, the error was removed by saving scripts in the project with the save button. By this, the latest version is reflected. Can you try to save the script of project (Google Apps Script) and run again? If this was not the direct solution of your issue, I apologize. – Tanaike Apr 22 '19 at 22:02

0 Answers0