· ruby

Ruby: Create and share Google Drive Spreadsheet


require 'sinatra'
require 'json'
require "google_drive"
require 'google/api_client'

CLIENT_ID = 'my client id'
CLIENT_SECRET = 'my client secret'
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/'
REDIRECT_URI = 'http://localhost:9393/oauth2callback'

helpers do
  def partial (template, locals = {})
    haml(template, :layout => false, :locals => locals)
  end
end

enable :sessions

get '/' do
  haml :index
end

configure do
  google_client = Google::APIClient.new
  google_client.authorization.client_id = CLIENT_ID
  google_client.authorization.client_secret = CLIENT_SECRET
  google_client.authorization.scope = OAUTH_SCOPE
  google_client.authorization.redirect_uri = REDIRECT_URI

  set :google_client, google_client
  set :google_client_driver, google_client.discovered_api('drive', 'v2')
end


post '/login/' do
  client = settings.google_client
  redirect client.authorization.authorization_uri
end

get '/oauth2callback' do
  authorization_code = params['code']

  client = settings.google_client
  client.authorization.code = authorization_code
  client.authorization.fetch_access_token!

  oauth_token = client.authorization.access_token

  session[:oauth_token] = oauth_token

  redirect '/'
end

%html
  %head
    %title Google Docs Spreadsheet
  %body
    .container
      %h2
        Create Google Docs Spreadsheet

      %div
        - unless session['oauth_token']
          %form{:name => "spreadsheet", :id => "spreadsheet", :action => "/login/", :method => "post", :enctype => "text/plain"}
            %input{:type => "submit", :value => "Authorise Google Account", :class => "button"}
        - else
          %form{:name => "spreadsheet", :id => "spreadsheet", :action => "/spreadsheet/", :method => "post", :enctype => "text/plain"}
            %input{:type => "submit", :value => "Create Spreadsheet", :class => "button"}


post '/spreadsheet/' do
  client = settings.google_client
  if session[:oauth_token]
    client.authorization.access_token = session[:oauth_token]
  end

  google_drive_session = GoogleDrive.login_with_oauth(session[:oauth_token])

  spreadsheet = google_drive_session.create_spreadsheet(title = "foobar")
  ws = spreadsheet.worksheets[0]

  ws[2, 1] = "foo"
  ws[2, 2] = "bar"
  ws.save()

  file_id = ws.worksheet_feed_url.split("/")[-4]

  drive = settings.google_client_driver

  new_permission = drive.permissions.insert.request_schema.new({
      'value' => "some_other_email@gmail.com",
      'type' => "user",
      'role' => "reader"
  })

  result = client.execute(
    :api_method => drive.permissions.insert,
    :body_object => new_permission,
    :parameters => { 'fileId' => file_id })

  if result.status == 200
    p result.data
  else
    puts "An error occurred: #{result.data['error']['message']}"
  end

  "spreadsheet created and shared"
end
  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket