2015-01-04 72 views
3

我是Rails的新手,我想从我的Web应用程序将数据导出到Google Spread Sheet。Rails:在Google电子表格中导出数据

  1. 我已经创建了一个应用程序来获取客户端ID和客户端的秘密,并启用了驱动器API。
  2. 我已经安装了谷歌驱动器和谷歌的API客户端宝石
  3. 我使用的代码表示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 

或有任何其他的方式做任务。如果我在错误的轨道?

+0

您是否可以在电子表格中写入数据?我能够获得访问令牌,但session.spreadsheets返回[] – Shweta 2015-09-17 16:32:54

+0

@Shweta,我最近写了一个gem用于导出你的** ActiveRecord/Mongoid **模型记录到Google表格:https://github.com/lakesare/model_to_googlesheet,这可能有所帮助。 – lakesare 2015-12-25 22:46:25

回答

0

我也是在我的项目之一对抗这一点,终于让我找到soulution如下:

使用客户端ID和客户端相反的分泌可以用下服务帐户在google developer console生成认证P12关键。在这种情况下,您不需要在控制器中粘贴任何代码。

要生成P12关键

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

并使用下面的代码片段来获取访问令牌。

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 

我希望它能帮助你。

+0

获得的权限不够:( – 2015-01-05 09:01:13

+0

)您可以详细说明您的错误,比如您在什么时候发生了此错误,并且请贴上错误消息。您希望使用哪种API。请检查您是否在谷歌开发者控制台上启用了该API。 – Anuja 2015-01-09 09:13:15

相关问题