【问题标题】:Authorization to google drive api using Ruby使用 Ruby 授权 google drive api
【发布时间】:2013-04-10 09:33:12
【问题描述】:

我想制作一个简单的应用程序(例如服务帐户 - 在 google api 控制台中),它将文件发送到 google 驱动器。

我有这样的代码:

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 中的救援': Timeout::Error (Faraday::Error::TimeoutError)

当我取消注释代码和############## 行之间的注释代码时,可以将文件发送到 gDrive,但我必须从网络浏览器输入授权代码。

我想自动完成,所以我决定像服务帐户一样使用 gDrive

我尝试通过添加行来增加连接超时:

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

当然还有连接:请求中的参数之后的连接,但我遇到了另一个错误

`sysread_nonblock': 到达文件末尾 (Faraday::Error::ConnectionFailed)

【问题讨论】:

    标签: ruby google-api-client google-drive-realtime-api


    【解决方案1】:

    缺少 (ssl) 证书。 如何解决:https://gist.github.com/fnichol/867550

    【讨论】:

    • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
    • 答案是并且曾经是:“缺少证书” - 其他一切都是奖励,以便“拥有”30 个字符。
    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2011-11-21
    相关资源
    最近更新 更多