2

Google ドライブにファイルを送信する単純なアプリケーション (Google API コンソールのサービス アカウントなど) を作成したいと考えています。

私はこのようなコードを持っています:

require 'rubygems'
require 'google/api_client'
require 'launchy'

#extra
gem 'oauth2'
gem 'omniauth'

client = Google::APIClient.new({:application_name => "testdevelop",:application_version => "1.0"})
drive = client.discovered_api('drive', 'v2')

####################################################################################
# Initialize OAuth 2.0 client    
# client.authorization.client_id = '111062272758.apps.googleusercontent.com'
# client.authorization.client_secret = 's8j3VQwCvlyz2Hcpr06xrVfr'
# client.authorization.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
# client.authorization.scope = 'https://www.googleapis.com/auth/drive'
# uri = client.authorization.authorization_uri
# Launchy.open(uri)
# $stdout.write  "Enter authorization code: "
# client.authorization.code = gets.chomp
# client.authorization.fetch_access_token!

####################################################################################
key = Google::APIClient::KeyUtils.load_from_pkcs12('12355eaee706eb725ff5dd890b5c2bc39d536a53-privatekey.p12', 'notasecret')
client.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
  :audience => 'https://accounts.google.com/o/oauth2/token',
  :scope => 'https://www.googleapis.com/auth/prediction',
  :issuer => '312062272758-bg7s7ts9f3m11hjetboodre6hfg4qp8q@developer.gserviceaccount.com',
  :signing_key => key)
client.authorization.fetch_access_token!
#####################################################################################


# Make an API call
# Insert a file
file = drive.files.insert.request_schema.new({
  'title' => 'My document',
  'description' => 'A test document',
  'mimeType' => 'text/plain'
})

media = Google::APIClient::UploadIO.new('document.txt', 'text/plain')
result = client.execute(
  :api_method => drive.files.insert,
  :body_object => file,
  :media => media,
  :parameters => {
    'uploadType' => 'multipart',
    'alt' => 'json'})

# Pretty print the API result
jj result.data.to_hash

実行するとエラーが発生しました

「rbuf_fill でレスキュー」: タイムアウト::エラー (ファラデー::エラー::タイムアウトエラー)

コメント化されたコードと ############## の行の間のコメント コードのコメントを外すと、gDrive にファイルを送信することは可能ですが、Web ブラウザーから認証コードを入力する必要があります。

自動的に実行したいので、サービス アカウントのように gDrive を使用することにしました

次の行を追加して、接続タイムアウトを増やしてみました。

conn = Faraday.default_connection
conn.options[:timeout] = 500 

もちろん、接続あり: リクエストのパラメータの後に conn がありますが、別のエラーが発生しました

`sysread_nonblock': ファイルの終わりに達しました (Faraday::Error::ConnectionFailed)

4

1 に答える 1

0

(ssl) 証明書がありません。修正方法: https://gist.github.com/fnichol/867550

于 2013-05-16T14:05:46.103 に答える