gimite / google-spreadsheet-ruby

Ruby library to read/write Google Spreadsheet
Other
306 stars 52 forks source link

Making the Worksheet's cells feed url compatible with 2 legged OAuth #22

Closed nikosd closed 13 years ago

nikosd commented 13 years ago

Making the Worksheet's cells feed url compatible with 2 legged OAuth (where the xoauth_requestor_id is appended to the url and it's also sent back from the Google servers).

This is a dirty hack to make this thing work. If you have any better idea please share it also with me :)

P.S.: I will try to post a test/scenario that points out the problem and proves the solution. Since this needs some pre-work (publish another mini-gem for ruby-oauth with 2 legged OAuth support) I'm not sure when this will be done. Until then, here are some hints :

gimite commented 13 years ago

Thanks for the patch.

What exactly is the issue using 2 legged OAuth without your patch? Probably it raises exception getting worksheet_feed_url? If that's the only issue, probably we should fix the pattern matching part in worksheet_feed_url instead. If Google server consistently add and accept URLs with xoauth_requestor_id parameter, I guess most of other places just works.

nikosd commented 13 years ago

From what I've remember that was the first thing that went wrong, so my first approach was to indeed change the regex.

Then, when I tried to update the cells of a worksheet I fell into a really really tedious and esoteric bug which was related to the cells feed fetching with the return-empty param. In essence the problem was that somewhere on the 2-3 parts of the code that mess with the url (google-spreadsheet,oath, and two-legged-oauth) was adding a double question mark on the url and google didn't return entry elements for the empty cells. The code was breaking when trying to call save since it din't know the id of the cell entry.

I'm pretty sure a test case would be much better and self-explanatory that the above paragraph so I will try to do it later - but as usual, no spare time :(

gimite commented 13 years ago

I see. Sounds like we should fix the part to add double question mark.

nikosd commented 13 years ago

Ok, I will try to write a test/best fix within the next hour. Will let you know.

nikosd commented 13 years ago

Added a test that proves the (ugly) fix. To actually run the test you will need the following :

Since all these may be too overwhelming (for Christ's sake it is!) to just run a test this is the output from the logger regarding the request urls :

Before the fix :

/Users/nikosd/.rvm/rubies/ree-1.8.7-2011.03/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/test/test_google_spreadsheet.rb --name=test_two_legged_oauth
Testing started at 6:07 PM ...
This test will create spreadsheets with your account, read/write them
and finally delete them (if everything goes well).
Original OAuth request path : https://docs.google.com/feeds/documents/private/full
Hacked OAuth request path : https://docs.google.com/feeds/documents/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/spreadsheets/private/full/SOME-KEY
Hacked OAuth request path : https://spreadsheets.google.com/feeds/spreadsheets/private/full/SOME-KEY?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos@fraudpointer.com
Hacked OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos@fraudpointer.com?return-empty=true&min-row=1&max-row=5&min-col=1&max-col=10
Hacked OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full??return-empty=true&min-row=1&max-row=5&min-col=1&max-col=10&xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com

Exception raised:
Class: <NoMethodError>
Message: <"undefined method `css' for nil:NilClass">
---Backtrace---
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/lib/google_spreadsheet.rb:899:in `save'
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/lib/google_spreadsheet.rb:896:in `each'
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/lib/google_spreadsheet.rb:896:in `save'
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/lib/google_spreadsheet.rb:888:in `each_slice'
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/lib/google_spreadsheet.rb:888:in `save'
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/test/test_google_spreadsheet.rb:131:in `test_two_legged_oauth'
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/test/test_google_spreadsheet.rb:130:in `test_two_legged_oauth'
/Users/nikosd/Library/Application Support/IntelliJIdea10/ruby/rb/testing/patch/testunit/test/unit/ui/testrunnermediator.rb:36:in `run_suite'
/Users/nikosd/Library/Application Support/IntelliJIdea10/ruby/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:108:in `start_mediator'
/Users/nikosd/Library/Application Support/IntelliJIdea10/ruby/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:96:in `start'
---------------
/Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/test/test_google_spreadsheet.rb:130:in `test_two_legged_oauth'
    /Users/nikosd/Library/Application Support/IntelliJIdea10/ruby/rb/testing/patch/testunit/test/unit/ui/testrunnermediator.rb:36:in `run_suite'
    /Users/nikosd/Library/Application Support/IntelliJIdea10/ruby/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:108:in `start_mediator'
    /Users/nikosd/Library/Application Support/IntelliJIdea10/ruby/rb/testing/patch/testunit/test/unit/ui/teamcity/testrunner.rb:96:in `start'
1 tests, 6 assertions, 1 failures, 0 errors
Test suite finished: 14.314567 seconds

Process finished with exit code 1

basically fails at :

assert_nothing_raised do
  worksheet.save
end

After the fix :

/Users/nikosd/.rvm/rubies/ree-1.8.7-2011.03/bin/ruby -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/nikosd/CodePlayground/Ruby/google-spreadsheet-ruby/test/test_google_spreadsheet.rb --name=test_two_legged_oauth
Testing started at 6:09 PM ...
This test will create spreadsheets with your account, read/write them
and finally delete them (if everything goes well).
Original OAuth request path : https://docs.google.com/feeds/documents/private/full
Hacked OAuth request path : https://docs.google.com/feeds/documents/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/spreadsheets/private/full/SOME-KEY
Hacked OAuth request path : https://spreadsheets.google.com/feeds/spreadsheets/private/full/SOME-KEY?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?return-empty=true&min-row=1&max-row=5&min-col=1&max-col=10
Hacked OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?return-empty=true&min-row=1&max-row=5&min-col=1&max-col=10&xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full/batch
Hacked OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full/batch?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/worksheets/SOME-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full
Hacked OAuth request path : https://spreadsheets.google.com/feeds/cells/SOME-KEY/SOME-OTHER-KEY/private/full?xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
Original OAuth request path : https://docs.google.com/feeds/documents/private/full/spreadsheet%3ASOME-KEY?delete=true
Hacked OAuth request path : https://docs.google.com/feeds/documents/private/full/spreadsheet%3ASOME-KEY?delete=true&xoauth_requestor_id=n.dimitrakopoulos%40fraudpointer.com
1 tests, 56 assertions, 0 failures, 0 errors
Test suite finished: 15.099691 seconds

Process finished with exit code 0

Hope this helps a little bit more. Let me know if there's anything else I can do.

gimite commented 13 years ago

I submitted some fixes: https://github.com/gimite/google-spreadsheet-ruby/commit/0aa9fa22fcbce3413e220c2c8aa9279145a13896

It will hopefully fix your issue, but I haven't run your test, so there may be still other issues. Please let me know if you still see errors.