我无法弄清楚这里的问题是什么。我有一个Rails / ActiveAdmin应用程序,我设置了一个任务。任务包含在rails任务中。
upload.rb
require 'google/apis/drive_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'certified'
require 'fileutils'
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME = 'Google Sync'
CLIENT_SECRETS_PATH = 'client_secret.json'
CREDENTIALS_PATH = File.join(Dir.home, '.credentials', "app-sync.yaml")
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE_FILE
##
## 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
FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
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"
puts 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
Drive = Google::Apis::DriveV3 # Alias the module
drive = Drive::DriveService.new
drive.client_options.application_name = APPLICATION_NAME
drive.authorization = authorize
# Upload a file
# Instead of passing a name for a file on the file system, create a pseudo file from a string
file_contents = StringIO.new(<<FILE_CONTENTS)
Field1,Field2,Field3
Some,Useless,Data
More,Useless,Info
FILE_CONTENTS
parent_folder_id = 'abcde...'
new_file = Drive::File.new(name: 'Test Upload CSV', parents: [parent_folder_id])
new_file = drive.create_file(new_file, upload_source: file_contents, content_type: 'text/csv')
puts "File '#{new_file.name}' created with id '#{new_file.id}'. Folder Location: https://drive.google.com/drive/folders/#{parent_folder_id}"
上面的代码会在使用ruby upload.rb
但是,当我将其包装到rake任务并运行rake google_test
时,它会返回以下错误
rake aborted!
Google::Apis::ServerError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
C:/Users/Brian/Path/To/App/lib/tasks/member_status_tasks.rake:122:in `block (2 levels) in <top (required)>'
Faraday::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
C:/Users/Path/To/App/lib/tasks/member_status_tasks.rake:122:in `block (2 levels) in <top (required)>'
OpenSSL::SSL::SSLError: SSL_connect SYSCALL returned=5 errno=0 state=SSLv2/v3 read server hello A
C:/Users/Path/To/App/lib/tasks/member_status_tasks.rake:122:in `block (2 levels) in <top (required)>'
Tasks: TOP => reps_exports:member_status
(See full trace by running task with --trace)
它引用的行也是new_file = drive.create_file(new_file, upload_source: file_contents, content_type: 'text/csv')
任何人都知道为什么这个作为副脚本运行时工作正常但是一旦作为rake任务添加到项目中它失败说服务器不接受SSL版本?我甚至尝试过指定TLSv1_2
,但仍然会出错returned=5
在Windows平台上安装的当前openssl版本是1.0.1j,Ruby 2.3.1和Rails 5.0.0.1