ruby-on-rails - rails : Export data in google spreadsheet

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

我是 Rails 的新手,我想将数据从我的网络应用程序导出到 Google Spread Sheet。

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

此代码成功运行,打开一个新的授权选项卡,并显示要粘贴的代码。这就是我卡住的地方。谷歌授权要求的代码在我的 Controller 代码中,因此我的用户可以将该代码粘贴到我的 Controller 中。我知道问这个问题很愚蠢,但我没有找到一种方法来自动从 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

如果我走错了路,还有其他方法可以完成任务吗?

最佳答案

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

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

生成p12 key

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

并使用以下代码片段获取访问 token 。

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(在以下代码片段中使用)将是在 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

希望对您有所帮助。

关于ruby-on-rails - rails : Export data in google spreadsheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764544/

相关文章:

ruby - 告诉 sorbet Sequel 模型的继承层次结构

javascript - 谷歌应用程序脚本 : How to fix "content is not allowed in prolog"

javascript - 自定义编码字符串

ruby-on-rails - ruby compass 错误 : Failed to build gem native extension

ruby-on-rails - 如何在符号或字符串上有条件地停止 pry.binding?

ruby-on-rails - rails 3 : How do I delay content submission so users can't spam rapidly?

ruby-on-rails - Rails 嵌套事务

ruby-on-rails - Rails4 和 Devise(NoMethod 错误)

ruby-on-rails - 如何在 Rails 4.0 中仅继承 ActiveRecord 模型的连接

javascript - 在拆分中使用析取