Ruby google_drive gem oAuth2 saving
I figured out this mess.
auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
The auth.scope is missing a url. REFERENCE from github
auth.scope =
"https://docs.google.com/feeds/" +
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
You can reuse your access_token, but first you need to get it. WARNING: The auth.access_token is only good for an hour. If you need another one you need to call auth.refresh! This will issue you another access_token.
require 'google/api_client'
require 'google_drive'
client = Google::APIClient.new(
:application_name => 'Example Ruby application',
:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = "YOUR CLIENT ID"
auth.client_secret = "YOUR CLIENT SECRET"
auth.scope =
"https://www.googleapis.com/auth/drive " +
"https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
print("1. Open this page:\n%s\n\n" % auth.authorization_uri)
print("2. Enter the authorization code shown in the page: ")
auth.code = $stdin.gets.chomp
auth.fetch_access_token!
access_token = auth.access_token
system'clear'
print "Save your access token\n\n"
print access_token
print "\nSave your refresh token\n\n"
print auth.refresh_token
This chunk of code should print out your access_token/refresh_token in your console. Now you can hard code your application.
require "google/api_client"
require "google_driver"
client = Google::APIClient.new(
:application_name => 'Example Ruby application',
:application_version => '1.0.0'
)
auth = client.authorization
auth.client_id = "Your Client ID"
auth.client_secret = "Your client secret"
access_token = "token you saved from the terminal"
session = GoogleDrive.login_with_oauth(access_token)
for file in session.files
p file.title
end
And eventually your "access_token" will expire. At this point you need to "refresh" it. You can call to see if your access_token is expired by calling
auth.expires_at
You can get a new access token with this example.
require 'google/api_client'
require 'google_drive'
$client_id = "YOUR CLIENT ID"
$client_secret = "YOUR CLIENT SECRET"
$refresh_token = "SAVED REFRESH TOKEN"
client = Google::APIClient.new(:application_name => 'Example Ruby application', :application_version => '1.0.0')
auth = client.authorization
auth.client_id = $client_id
auth.client_secret = $client_secret
auth.scope = "https://docs.google.com/feeds/" + "https://www.googleapis.com/auth/drive " + "https://spreadsheets.google.com/feeds/"
auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
auth.refresh_token = $refresh_token
#here's the magic sauce
auth.refresh!
#as you can see above auth.access_token wasn't passed a value
# but if you call it now you'll see you have a new access_token
auth.access_token
=> new token from server
Does Omniauth-google-oauth2 simply allow authentication, or does it also address API needs?
I think of Oauth2 as a "way to get the user's password to confirm their existence on my site".
So instead of your User model having a password column, in essence, it uses Google to say "this guy is cool".
Now, what does that have to do with API calls, you wonder... me too.
If I recall, there is a Refresh token that lasts for more than the 20 ms of authetication and will allow you to access their Google Docs, if Google's api allows you to do that.
Having said all that, If google needs their token, plus your API token to access their spreadsheet, I'd stick it into the session.
But if their API said to stick spreadsheet in the scope, then it must say something about how to use it all together too, no?
More Edits
Google Spreadsheets Oauth 2.0 authentication piece is here, with a flow. Notice the part about refresh tokens. I'd look into that.
It says to store it somewhere, which I'd choose the session, or if you are totally paranoid a db column somewhere, but not sure if that is right either. Just spitballing here.
Final Edit
Turns out even the people helping out the Oauth 2.0 don't agree/get it conceptually either.
Ruby Google Drive Authorization Error when Writing to a File
It's a known issue: https://github.com/gimite/google-drive-ruby/issues/34
download_to_file uses different API, and it seems it only supports
downloading as HTML. To download as PDF, you need to write code to
fetch the URL you mentioned manually. You can probably use
session.request() method to reuse the authorization google-drive-ruby
provides.It would be nice to have that feature in google-drive-ruby. But the
API google-drive-ruby uses (Document List API) will be deprecated, so
I need to switch to Google Drive API. So I will implement that feature
after the switch (if it's available in Google Drive API).
Related Topics
How to Generate a Wsdl Using Ruby
Rails Initializes Extremely Slow on Ruby 1.9.1
Rails How to List All Images in a Folder Using the Image_Tag
Need Help Maximizing 3 Factors in Multiple, Similar Objects and Ordering Appropriately
How Does Ruby Allow a Method and a Class with the Same Name
How to Evaluate a Date Difference in Years, Months and Days (Ruby)
2 Gems Need Different Versions of the Same Dependency
Rails - How to Check for Online Users
Sidekiq Worker Not Getting Triggered
Rails Devise - Current_User Is Nil
Ruby Errors with Os X Yosemite
Configuration in Allure to Cucumber in Ruby
Bitwise Operations on Strings with Ruby