I'm trying to update a Google Workspace Sheet using a client in Ruby on macOS. I can't find a sample of this that currently works. I am currently trying the following, which is obviously incorrect! When it executes:
user_credentials.fetch_access_token!
I get the error:
ERROR Signet::AuthorizationError: Authorization failed. Server message:\n{\n "error": "unsupported_grant_type",\n "error_description": "Invalid grant_type: "\n}
/Users/jtosey/.rbenv/versions/3.1.3/lib/ruby/gems/3.1.0/gems/signet-0.17.0/lib/signet/oauth_2/client.rb:1028:in `fetch_access_token'
This is my current authentication code:
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'launchy'
require 'webrick'
# Configuration
secrets = ENV['HOME'] + '/.secrets'
credentials_file = secrets + '/google_workspace_client_secret.json'
token_store_file = secrets + '/google_workspace_token.yaml'
# Authorization
scopes = ['https://www.googleapis.com/auth/spreadsheets']
client_id = Google::Auth::ClientId.from_file(credentials_file)
token_store = Google::Auth::Stores::FileTokenStore.new(file: token_store_file)
authorizer = Google::Auth::UserAuthorizer.new(client_id, scopes, token_store)
user_credentials = Google::Auth::UserRefreshCredentials.new(client_id: client_id.id, scope: scopes, token_store: token_store)
if user_credentials.refresh_token.nil?
auth_uri = user_credentials.authorization_uri(
access_type: 'offline',
prompt: 'select_account consent',
redirect_uri: 'http://localhost:8080'
)
Launchy.open(auth_uri)
root = File.expand_path '~/public_html'
server = WEBrick::HTTPServer.new Port: 8080, DocumentRoot: root
server.mount_proc '/' do |req, res|
code = req.query['code']
if code.nil?
[200, {'Content-Type' => 'text/html'}, [%(<html><body><h2>Authorization required</h2><p>Click <a href="#{auth_uri}">here</a> to authorize the application.</p></body></html>)]]
else
user_credentials.code = code
user_credentials.fetch_access_token!
user_credentials.store(token_store)
[200, {'Content-Type' => 'text/html'}, ['<html><body><h2>Authorization successful!</h2></body></html>']]
# TODO: stop server
end
end
server.start
end
Suggestions?