Search code examples
ruby-on-railsoauthgoogle-docsgoogle-docs-api

OAuth issues with Google Docs API using Ruby (Rails 3.0.6)


We can't seem to create a new spreadsheet per the Google Docs API.

Our request merely hangs. We're on Rails 3.0.6.

Here's our code:

 msg = '<atom:entry xmlns:atom="http://www.w3.org/2005/Atom">
  <atom:category scheme="http://schemas.google.com/g/2005#kind"
                 term="http://schemas.google.com/docs/2007#document" />
  <atom:title>Company Perks</atom:title>
</atom:entry>'

oauth_base_string = 'POST&http://docs.google.com/feeds/documents/private/full&oauth_consumer_key=DOMAIN&oauth_nonce=1c4fbbe4387a685829d5938a3d97988c&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1227303732&oauth_version=1.0&xoauth_requestor_id=ID'  

key = 'KEY'

oauth_signature = CGI.escape(Base64.encode64("#{OpenSSL::HMAC.digest('sha1', key, oauth_base_string)}"))

headers = { 
  'Content-Type' => 'application/atom+xml',
  'Authorization' => 'OAuth oauth_consumer_key="DOMAIN",oauth_nonce"1c4fbbe4387a685829d5938a3d97988c",oauth_signature=' + oauth_signature + ',oauth_signature_method="HMAC-SHA1",oauth_timestamp="1227303732",oauth_version="1.0"'
}

uri = URI.parse 'http://docs.google.com/feeds/documents/private/full?xoauth_requestor_id=ID'
http = Net::HTTP.new uri.host, uri.port
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
resp, data = http.post(uri.request_uri, msg, headers)

Legend:

DOMAIN = OAuth consumer key ID = the email address for whom we're trying to create a new spreadsheet KEY = OAuth consumer secret


Solution

  • Is there any particular reason you're trying to write it manually? If I were to guess, it'd be because your timestamp and nonce are hardcoded. Also, IIRC Google uses Oauth 2.0 and supports 1.0a, so you'll likely need a request and access token. From you example, I don't see that you are fetching it.

    IMO, You're likely better off using a gem.

    Omniauth is a fairly popular gem that does exactly what you need, and included the Google oauth strategy: https://github.com/intridea/omniauth

    Also, I've used this gem in the past, and it was useful for my project. http://oauth.rubyforge.org/