1
我想制作一个简单的应用程序(如服务帐户 - 在谷歌api控制台),它将文件发送到谷歌驱动器。使用Ruby对谷歌驱动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 => '[email protected]account.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
当我运行它,我得到一个错误
`rescue in rbuf_fill': Timeout::Error (Faraday::Error::TimeoutError)
当我取消注释######的行之间的代码和注释代码########可以将文件发送到gDrive,但我必须从网络浏览器输入授权码。
我想这样做,全自动,这是我决定使用的GDrive像一个服务帐户
我曾尝试通过增加线路,增加连接超时:
conn = Faraday.default_connection
conn.options[:timeout] = 500
和ofcourse与连接:康恩后请求中的参数,但我有另一个错误
`sysread_nonblock': end of file reached (Faraday::Error::ConnectionFailed)
虽然此链接可能回答问题,但最好在此处包含答案的重要部分并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – theTRON 2013-05-16 14:24:43
答案是,“证书缺失” - 其他一切都是奖金,以便“拥有”30个字符。 – inselberg 2013-05-16 21:18:34