So before the update there was a simple way to log into google drive and manipulate your google docs - with ruby.
Before you were able to log into your google drive with this.
require 'google_drive'
$session = GoogleDrive.login("[email protected]", "password")
But now you get the warning message:
WARNING: GoogleDrive.login is deprecated and will be removed in the next version. Use GoogleDrive.login_with_oauth instead.
So I went to the git hub page to see how google wants us to use their services with high level languages, and found out they want us to use oAuth2. Like so.
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
$session = GoogleDrive.login_with_oauth(access_token)
This is fine and all but I can't save the auth variable. I would like to hard code this in the script so I don't have to keep going to google to get a new access_token.
So I've tried to get the access_token and adding it to the script like so.
require 'google/api_client'
require 'google_drive'
client = Google::APIClient.new
access_token = "TokenFromGoogleHardCoded"
$session = GoogleDrive.login_with_oauth(access_token)
# $session doesn't connect
I'm not sure I'm attacking this problem in the correct manor. I would like to save the complete auth variable and hard code it in my scripts.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With