我对 export_file 方法有一些问题。它似乎不时工作 这是我的完整代码
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
SCOPE = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRETS_PATH = 'private/credentials-2.json'
TOKEN_PATH = 'private/tokens.yaml'
USER_ID = ENV['USER']
class GoogleDriveGetFiles
def call(file_name)
@file_name = file_name
credentials = auth
drive_service = drive_service_auth(credentials)
file_id = get_file(drive_service)
write_in_file(file_id, drive_service)
end
private
def auth
client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
token_store = Google::Auth::Stores::FileTokenStore.new(file: TOKEN_PATH)
authorizer = Google::Auth::UserAuthorizer.new(client_id, SCOPE, token_store)
credentials = authorizer.get_credentials(USER_ID)
if credentials.nil?
url = authorizer.get_authorization_url(base_url: OOB_URI)
puts "Open #{url} in your browser and enter the resulting code:"
code = gets.chomp
credentials = authorizer.get_and_store_credentials_from_code(
user_id: USER_ID, code: code, base_url: OOB_URI)
end
credentials
end
def drive_service_auth(credentials)
drive_service = Google::Apis::DriveV3::DriveService.new
drive_service.authorization = credentials
drive_service
end
def get_file(drive_service)
files = drive_service.list_files(fields: 'nextPageToken, files(id, name, mimeType)').files
file_id = nil
files.each do |file|
if file.name == @file_name
file_id = file.id
puts file.name
break
end
end
file_id
end
def write_in_file(file_id, drive_service)
# binding.pry
file = drive_service.get_file(file_id)
File.write("output/#{@file_name}.txt", drive_service.export_file(file_id, 'text/plain'))
puts 'Succefully write in file'
end
end
get_file 方法工作正常,但 export_file 不工作
我试过调试库但无法查明错误的确切原因。非常感谢任何有关如何解决此问题的帮助或建议!
我工作到晚上 7:30,现在我有错误。 这是我的错误堆栈跟踪。
/home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:239:in `check_status': badRequest: The requested conversion is not supported. (Google::Apis::ClientError)
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/api_command.rb:135:in `check_status'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:202:in `process_response'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:321:in `execute_once'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:126:in `block (2 levels) in do_retry'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:123:in `block in do_retry'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/retriable-3.1.2/lib/retriable.rb:61:in `block in retriable'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `times'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/retriable-3.1.2/lib/retriable.rb:56:in `retriable'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:113:in `do_retry'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/http_command.rb:104:in `execute'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-core-0.11.0/lib/google/apis/core/base_service.rb:418:in `execute_or_queue_command'
from /home/we/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/google-apis-drive_v3-0.37.0/lib/google/apis/drive_v3/service.rb:1064:in `export_file'
from /home/we/ruby/parse/google_drive_get_file.rb:64:in `write_in_file'
from /home/we/ruby/parse/google_drive_get_file.rb:19:in `call'
from main.ruby:10:in `<main>'