1

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

1 Answer 1

0

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/

2
  • Thanks, Cory. We hard coded this because all we need is 2-legged OAuth with Google Docs. We didn't want the extra baggage with a more general purpose gem. All we want to do is create a Google spreadsheet and write to it. Given our goals, do you think the extra weight of this gem is worth it? The API description for 2-legged authentication seems simple: code.google.com/apis/accounts/docs/OAuth.html#GoogleAppsOAuth
    – Crashalot
    Commented Jul 31, 2011 at 8:14
  • You're right, it isn't that difficult, although the gem is the quick and easy solution. If you want to roll your own, I'd suggest having a look at Oauth requests are supposed to work: code.google.com/apis/accounts/docs/OAuth_ref.html#SigningOAuth The basic auth flow is: 1) Obtain a request token, using OAuthGetRequestToken. 2) Authorize that request token, using OAuthAuthorizeToken. 3) Exchange the authorized request token for an access token, using OAuthGetAccessToken. Also pay attention on how to generate a nonce and sign requests.
    – Cory
    Commented Jul 31, 2011 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.