我是Rails的新手,我想从我的Web应用程序将数据导出到Google Spread Sheet。Rails:在Google电子表格中导出数据
- 我已经创建了一个应用程序来获取客户端ID和客户端的秘密,并启用了驱动器API。
- 我已经安装了谷歌驱动器和谷歌的API客户端宝石
- 我使用的代码表示here
这段代码成功运行,打开一个新标签进行授权,并显示代码粘贴。这是我卡住的地方。 Google授权要求的代码位于我的控制器代码中,因此我的用户可以将该代码粘贴到我的控制器中。我知道它很幽默,但我没有找到一种方法,像我们通常在我们的Facebook oauth应用程序中那样,自动从api获取代码以进一步执行。那么你能指导我如何去做?代码看起来像
def export_spred_sheet
require 'rubygems'
require 'google/api_client'
require 'launchy'
# Get your credentials from the console
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
# Create a new API client & load the Google Drive API
client = Google::APIClient.new
drive = client.discovered_api('drive', 'v2')
# Request authorization
client.authorization.client_id = CLIENT_ID
client.authorization.client_secret = CLIENT_SECRET
client.authorization.scope = OAUTH_SCOPE
client.authorization.redirect_uri = REDIRECT_URI
uri = client.authorization.authorization_uri
Launchy.open(uri)
# Exchange authorization code for access token
$stdout.write "Enter authorization code: "
client.authorization.code = gets.chomp
client.authorization.fetch_access_token!
# 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
end
或有任何其他的方式做任务。如果我在错误的轨道?
您是否可以在电子表格中写入数据?我能够获得访问令牌,但session.spreadsheets返回[] – Shweta 2015-09-17 16:32:54
@Shweta,我最近写了一个gem用于导出你的** ActiveRecord/Mongoid **模型记录到Google表格:https://github.com/lakesare/model_to_googlesheet,这可能有所帮助。 – lakesare 2015-12-25 22:46:25