Google Sheets API“(Google :: Apis :: ClientError)”

时间:2018-07-12 15:27:54

标签: ruby-on-rails ruby google-api google-sheets-api

我试图在我的Rails应用中测试Google Sheets API,我遵循了快速入门步骤https://developers.google.com/sheets/api/quickstart/ruby。当我运行quickstart.rb时,一切运行良好。 然后,我尝试使用我的一个驱动器帐户(与Google开发人员帐户相同)更改电子表格ID,但出现以下错误:

Traceback (most recent call last):
	15: from quickstart.rb:45:in `<main>'
	14: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/generated/google/apis/sheets_v4/service.rb:729:in `get_spreadsheet_values'
	13: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/base_service.rb:360:in `execute_or_queue_command'
	12: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/http_command.rb:93:in `execute'
	11: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
	10: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
	 9: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
	 8: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/http_command.rb:101:in `block in execute'
	 7: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
	 6: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
	 5: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
	 4: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/http_command.rb:104:in `block (2 levels) in execute'
	 3: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/http_command.rb:299:in `execute_once'
	 2: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/http_command.rb:183:in `process_response'
	 1: from /Users/eliseserres/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/api_command.rb:116:in `check_status'
/Users/EliseS/.rvm/gems/ruby-2.5.1/gems/google-api-client-0.23.3/lib/google/apis/core/http_command.rb:218:in `check_status': badRequest: Unable to parse range: Class Data!A2:C (Google::Apis::ClientError)

我忘记了什么吗?

这是我的quickstart.rb代码:

require 'google/apis/sheets_v4'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'

OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Google Sheets API Ruby Quickstart'.freeze
CLIENT_SECRETS_PATH = 'client_secret.json'.freeze
CREDENTIALS_PATH = 'token.yaml'.freeze
SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS 

##
# 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(CLIENT_SECRETS_PATH)
  token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
  authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
  user_id = 'default'
  credentials = authorizer.get_credentials(user_id)
  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::SheetsV4::SheetsService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize

# Prints the names and majors of students in a sample spreadsheet:
# https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
spreadsheet_id = '1MfecOlImuMVOkIt__8Yvm8uvFyMGlOzORq9Ep6EnnyE'
range = 'Class Data!A2:C'
response = service.get_spreadsheet_values(spreadsheet_id, range)
puts 'Name, Major:'
puts 'No data found.' if response.values.empty?
response.values.each do |row|
  # Print columns A and E, which correspond to indices 0 and 4.
  puts "#{row[0]}, #{row[2]}"
end

0 个答案:

没有答案