【问题标题】:Rails: Export data in google spreadsheetRails:在谷歌电子表格中导出数据
【发布时间】:2015-01-04 10:23:58
【问题描述】:

我是 Rails 新手,我想从我的网络应用程序将数据导出到 Google 电子表格。

  1. 我创建了一个应用程序来获取客户端 ID 和客户端密码,并为此启用了驱动器 API。
  2. 我已经安装了 google drive 和 google api client gem
  3. 我使用了here 中的代码

此代码成功运行,打开一个新选项卡进行授权,并显示要粘贴的代码。这就是我卡住的地方。谷歌授权要求的代码在我的控制器代码中,因此我的用户可以将该代码粘贴到我的控制器中。我知道问这个问题很愚蠢,但我没有找到一种方法来自动从 api 获取代码以进一步执行,就像我们通常在我们的 facebook oauth 应用程序中所做的那样。那你能指导我怎么做吗?代码看起来像

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,我最近写了一个 gem,用于将您的 ActiveRecord/Mongoid 模型记录导出到 googlesheets:github.com/lakesare/model_to_googlesheet,这可能会有所帮助。

标签: ruby-on-rails ruby google-sheets google-oauth


【解决方案1】:

我也在我的一个项目中与此作斗争,最后我找到了如下解决方案:

您可以使用在服务帐户下的google developer console 中生成的 P12 密钥来进行身份验证,而不是使用客户端 ID 和客户端密钥。在这种情况下,您不需要在控制器中粘贴任何代码。

生成p12密钥

  1. 转至Google developer console
  2. 然后 -> “API 和身份验证” -> “凭据”
  3. 创建“服务帐户”类型的新客户端 ID
  4. 一旦生成新的客户端 ID。在服务帐户部分下,您将找到一个“生成新 P12 密钥”的按钮。单击它会生成一个 p12 密钥。下载它并将其安全地存储在您的应用中并用于身份验证。

并使用以下代码 sn-p 获取访问令牌。

key_file = p12_key_file_path
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, 'notasecret')
client = Google::APIClient.new application_name: "abcd", application_version: "1.0.0"

SERVICE_ACCOUNT_ID(用于以下代码 sn-p)将是在 google developer console 的“服务帐户”部分下生成的电子邮件地址。

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/analytics',
    :issuer => SERVICE_ACCOUNT_ID,
    :access_type => 'offline',
    :signing_key => key
  )

  client.authorization.fetch_access_token!
  client

希望对你有所帮助。

【讨论】:

  • 获得的权限不足 :(
  • 您能否详细说明您的错误,例如您在什么时候收到此错误,请粘贴错误消息。您希望使用哪个 api。请检查您是否在 google 开发者控制台上启用了该 api。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多