2013-04-10 70 views
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)

回答

0

(ssl)-certificate丟失。 如何修復:https://gist.github.com/fnichol/867550

+0

雖然此鏈接可能回答問題,但最好在此處包含答案的重要部分並提供供參考的鏈接。如果鏈接頁面更改,則僅鏈接答案可能會失效。 – theTRON 2013-05-16 14:24:43

+0

答案是,「證書缺失」 - 其他一切都是獎金,以便「擁有」30個字符。 – inselberg 2013-05-16 21:18:34