Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

270
Views
Crear un archivo vacío con la API de Drive

Creo este código, usando esta referencia https://developers.google.com/drive/api/quickstart/ruby

 class Drive def initialize @drive_service = Google::Apis::DriveV3::DriveService.new @drive_service.client_options.application_name = APPLICATION_NAME @drive_service.authorization = authorize end def create_file data = { 'name': "My new Sheet #{Time.now.strftime('%d/%m/%Y %H:%M')}", 'mimeType': 'application/vnd.google-apps.spreadsheet' } @drive_service.create_file(data).execute end def share_file "to be filled" end def list_files response = @drive_service.list_files(page_size: 10, fields: 'nextPageToken, files(id, name)') puts 'Files:' puts 'No files found' if response.files.empty? response.files.each do |file| puts "#{file.name} (#{file.id})" end end end

El método list_files funciona bien, pero create_file me devuelve este error:

 Traceback (most recent call last): 2: from quickstart.rb:79:in `<main>' 1: from quickstart.rb:60:in `create_file' /home/vagrant/.rvm/gems/ruby-2.7.2/gems/google-api-client-0.53.0/generated/google/apis/drive_v3/service.rb:895:in `create_file': unknown keywords: :name, :mimeType (ArgumentError)

Lo creé en base a esta referencia de método de creación: https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/DriveV3/DriveService.html#create_file-instance_method pero aún no puedo hacerlo trabajo, ¿qué podría estar mal?

Intenté cambiar la creación a: @drive_service.create_file(file_object = data).execute

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Comparemos tu código,

 data = { 'name': "My new Sheet ... @drive_service.create_file(data).execute

vs. la firma del método documentado .

 `create_file(file_object = nil, enforce_single_parent: nil, ignore_default_visibility: nil, include_permissions_for_view: nil, keep_revision_forever: nil, ocr_language: nil, supports_all_drives: nil, supports_team_drives: nil, use_content_as_indexable_text: nil, fields: nil, quota_user: nil, user_ip: nil, upload_source: nil, content_type: nil, options: nil) {|result, err| ... } ⇒ Google::Apis::DriveV3::File` - file_object (Google::Apis::DriveV3::File) (defaults to: nil)

El primer argumento debe ser una instancia de Google::Apis::DriveV3::File , pero está pasando un Hash .

over 4 years ago · Santiago Trujillo Report

0

Según esta documentación :

Al proporcionar hashes para objetos de solicitud. Si es el último argumento de un método, algunas versiones de Ruby interpretarán el hash como argumentos de palabras clave. Para evitar esto, agregar un hash vacío como parámetro adicional evitará interpretaciones erróneas.

 file = {id: '123', title: 'My document', labels: { starred: true }} file = drive.create_file(file) # Raises ArgumentError: unknown keywords: id, title, labels file = drive.create_file(file, {}) # Returns a Drive::File instance

En su código, simplemente agregue {} como segundo parámetro para create_file.

Desde:

 @drive_service.create_file(data)

Para:

 @drive_service.create_file(data, {})

Avíseme si esto funciona en su extremo.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!