Ruby Net::HTTP issues accessing Google Docs API v3

I’m having trouble using Net::HTTP in Ruby to access the Google Docs List Data API v3. Here’s what I’ve tried:

require 'net/http'

def fetch_data(url, auth_headers)
  uri = URI.parse(url)
  Net::HTTP.start(uri.host, uri.port) do |http|
    http.get(uri.path, auth_headers)
  end
end

def authenticate(service)
  http = Net::HTTP.new('accounts.google.com', 443)
  http.use_ssl = true
  auth_data = "account_type=GOOGLE&email=#{USER}&password=#{PASS}&service=#{service}"
  headers = { 'Content-Type' => 'application/x-www-form-urlencoded' }
  response = http.post('/AccountLogin', auth_data, headers)
  
  { "Authorization" => "GoogleLogin token=#{response.body[/Token=(.*)/, 1]}",
    "API-Version" => "3.0" }
end

puts fetch_data('https://sheets.googleapis.com/v4/spreadsheets', authenticate('sheets'))
puts fetch_data('https://docs.googleapis.com/v1/documents', authenticate('docs'))

It works fine for the Sheets API but fails for the Docs API with an EOFError. Any ideas what’s going wrong?

Hey there, I’ve dealt with similar issues when working with Google APIs in Ruby. The problem you’re facing is likely due to using the deprecated ClientLogin method. Google has phased this out in favor of OAuth 2.0 for better security.

For your Docs API issue, I’d recommend switching to OAuth 2.0. It’s a bit more complex to set up initially, but it’s way more reliable and secure. You’ll need to create a project in the Google Cloud Console, enable the Docs API, and get your client ID and secret.

Here’s a quick tip: Use the ‘google-auth-library-ruby’ gem. It handles a lot of the OAuth complexity for you. You can install it with:

gem install google-auth-library

Then, you can use it to handle the authentication flow. Once you’ve got your access token, you can use it with Net::HTTP or even better, switch to a gem like ‘httparty’ for cleaner HTTP requests.

Trust me, once you make this switch, you’ll find it much easier to work with both the Docs and Sheets APIs consistently. Good luck with your project!

I see you’re struggling with the Google Docs API using Net::HTTP in Ruby. Your code looks outdated, especially the authentication part. Google has moved away from ClientLogin to OAuth 2.0 for security reasons.

For the Docs API, you’ll need to update your authentication method. Here’s what I’d suggest: set up a project in Google Cloud Console, enable the Google Docs API, create OAuth 2.0 credentials (client ID and secret), and use a library like ‘google-auth-library-ruby’ to handle the OAuth flow.

Once you’ve obtained your access token, you can use it with Net::HTTP or consider a more user-friendly gem like ‘httparty’. This approach should work for both Sheets and Docs APIs. The EOFError you’re encountering is likely due to the API rejecting the old authentication method; switching to OAuth 2.0 should resolve this issue.

hey man, i feel ur pain with google apis. they can be a real pain sometimes. ive had similar issues before. looks like ur using an old auth method thats not supported anymore. u need to switch to oauth 2.0 for both sheets and docs apis. its a bit more work to set up but itll fix ur problem. good luck!