📂 Store Your Ruby-Generated Files on Google Drive Effortlessly!

March 7, 2025

Integrating Google Drive API with Ruby seemed straightforward—until it wasn’t. From cryptic exceptions to OAuth nightmares, I hit some tricky roadblocks. Here’s how I debugged my way to success and what I learned along the way.


📢 Need a Smarter Way to Store Files on Google Drive?

If you’re looking to enhance your application by seamlessly saving files to Google Drive, I can help! Whether it’s automating spreadsheets, managing file storage, or integrating Drive with your Ruby application, I offer efficient and affordable solutions tailored to your needs.

💡 Let’s connect and discuss how to streamline your workflow! >>>


The Challenge

I set out to automate Google Sheets creation with Ruby. Easy, right? Well, not exactly. Here’s what stood in my way:

  • Mysterious LoadError and NameError exceptions
  • OAuth verification hurdles
  • “API not enabled” errors
  • Missing refresh_token issues

Let’s break it down. 🔍


🛠 Step 1: Setting Up the Environment

The first hiccup? The google_drive gem clashed with nokogiri, throwing dependency errors. The fix:

# Install compatible gems
gem install nokogiri --platform=ruby -- --use-system-libraries
gem install google_drive

💡 Key Takeaway: Always check gem compatibility with your Ruby version (I used 3.4).


🔑 Step 2: Taming OAuth Authentication

The biggest beast in this journey? OAuth authentication. I needed a refresh_token, but generating one manually was a hassle. So, I automated the process:

require "google_drive"

session = GoogleDrive::Session.new(
  client_id: "YOUR_CLIENT_ID",
  client_secret: "YOUR_CLIENT_SECRET",
  scope: ["https://www.googleapis.com/auth/drive"]
)

auth_url = session.auth_client.authorization_uri(
  redirect_uri: "urn:ietf:wg:oauth:2.0:oob",
  scope: session.scope
).to_s

puts "1. Open this URL: #{auth_url}"
print "2. Enter authorization code: "
code = gets.chomp

session.auth_client.code = code
session.auth_client.fetch_access_token!

File.write("config.json", JSON.dump({
  client_id: session.client_id,
  client_secret: session.client_secret,
  refresh_token: session.auth_client.refresh_token
}))

Pro Tip: Add your Google account as a test user to bypass app verification delays.


🔄 Step 3: Enabling APIs (Because Google Won’t Do It for You!)

When I saw PERMISSION_DENIED, I realized the Google Sheets API wasn’t enabled. The fix:

  1. Go to Google Cloud Console
  2. Enable Google Drive API and Google Sheets API

If you forget this step, be prepared for hours of debugging! 😅


💻 The Final Code: Creating a Spreadsheet

With authentication sorted, I automated spreadsheet creation:

require "google_drive"

session = GoogleDrive::Session.from_config("config.json")

spreadsheet = session.create_spreadsheet(title: "My Automated Sheet")
worksheet = spreadsheet.worksheets.first

worksheet.update_cells(1, 1, [
  ["Name", "Email"],
  ["Alice", "alice@example.com"]
])

worksheet.save
puts "✅ Sheet created successfully!"

🎉 Success! A fully automated Google Sheet, powered by Ruby.


🌟 Lessons Learned

  • OAuth is tricky but manageable – Use service accounts for server-to-server automation.
  • API enablement is critical – Always double-check enabled APIs in Google Cloud Console.
  • Error messages are clues – LoadError often means dependency issues, not faulty code.

Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>

Need Expert Ruby on Rails Developers to Elevate Your Project?

🚀 What’s Next?

I’m now diving into:

✅ Automating data imports from APIs to Sheets ✅ Building a CLI tool for spreadsheet management ✅ Experimenting with Google Drive file organization

Have you faced similar integration challenges? Let’s connect and share solutions! 🔥

📌 Key Tools Used:

  • Gems: google_drive, nokogiri
  • Google Cloud Console
  • OAuth 2.0 flow

👍 Liked this post? Follow me for more technical deep-dives!

Leave a comment