Service account authentication.
I'm trying to authenticate google calendar with a service account and have run into issues.
The current issue I've encountered is an error stating:
Authorization failed. Server message: { "error" : "invalid_request", "error_description" : "Required parameter is missing: grant_type" }
This is my current authorization code:
# Initialize the client
client = Google::APIClient.new(application_name: 'APP Name')
# Load & decrypt private key
key = Google::APIClient::KeyUtils.load_from_pkcs12(ENV['GOOGLE_APPLICATION_CREDENTIALS'], 'notasecret')
# Generate request body for authorization
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/calendar',
:issuer => ENV['GOOGLE_CLIENT_EMAIL'],
:person => ENV['GOOGLE_CALENDAR_EMAIL'],
:access_type => 'offline',
:signing_key => key
)
# fetch access token
client.authorization.fetch_access_token!
And my call is:
# load API definition
service = client.discovered_api('calendar', 'v3')
google_event = { 'summary' => @event.summary, 'description' => @event.description, 'start' => {'date' => @event.start_date}, 'end' => {'date' => @event.end_date} }
result = client.execute(:api_method => service.events.insert,
:parameters => {'calendarId' => ENV['GOOGLE_CALENDAR_ID'], 'sendNotifications' => true},
:body => JSON.dump(google_event),
:grant_type => 'refresh_token',
:headers => {'Content-Type' => 'application/json'}
)
Any ideas?
Can you try using google-auth-library-ruby?
For service account auth, you can use the application default credentials flow by pointing the env var GOOGLE_APPLICATION_CREDENTIALS to the path of your JSON key.
See https://developers.google.com/identity/protocols/application-default-credentials#howtheywork
Then you can authorize like this: https://github.com/google/google-auth-library-ruby#example-usage
@saicheems Thanks for your response.
I have tried that. It did not work either. After that failed I was directed to try using Signet instead.
This is what I had:
scopes = 'https://www.googleapis.com/auth/calendar'
authorization = Google::Auth.get_application_default(scopes)
cal = Google::Apis::CalendarV3::CalendarService.new
cal.authorization = authorization
cal.authorization.fetch_access_token!
And here is the error I got using google-auth-library-ruby

So the auth library raises this error if credentials could not be found in the default locations. The library checks your env vars, a well known path, and a system path in that order.
See: https://github.com/google/google-auth-library-ruby/blob/37ed189b2e5165243918fcde127ebe323e9a06b5/lib/googleauth.rb#L45 https://github.com/google/google-auth-library-ruby/blob/37ed189b2e5165243918fcde127ebe323e9a06b5/lib/googleauth.rb#L114
Did you set GOOGLE_APPLICATION_CREDENTIALS?
ex: export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/key.json
Yes I did set GOOGLE_APPLICATION_CREDENTIALS to the path of the key.json file which was in the /config directory.
I also set ENV variables for GOOGLE_ACCOUNT_TYPE, GOOGLE_CLIENT_ID, GOOGLE_CLIENT_EMAIL & GOOGLE_PRIVATE_KEY.
And are you using a json file as your key? I see in your first comment you loaded a p12 format key, can you try downloading a json service account key and pointing the env var to that?
I was using a json service account key when I was trying to authenticate with google-auth-library-ruby, but since that json key wasn't being found I switched to using Signet with the p12 key.
@saicheems I am trying again with google-auth-library, but I'm seeing this error now:
cannot load such file -- google/apis/calendar_v3 from the line require 'google/apis/calendar_v3'
My gemfile has:
gem 'google-api-client', '0.9.1', require: ['google/apis/calendar_v3', 'google/apis/plus_v1']
So I've tried this on my machine:
Gemfile:
source 'https://rubygems.org'
gem 'google-api-client', '~> 0.10'
sample.rb:
require 'google/apis/calendar_v3'
scopes = 'https://www.googleapis.com/auth/calendar'
authorization = Google::Auth.get_application_default(scopes)
cal = Google::Apis::CalendarV3::CalendarService.new
cal.authorization = authorization
puts cal.authorization.fetch_access_token!
Download a fresh service credentials key in JSON format from https://console.developers.google.com/project/_/apis/credentials and save that in the same directory as your sample as key.json.
bundle install
export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/key.json
@saicheems Thanks! I guess my old jsonfile must have been bad. The new one authenticates. I'll have to commit it into git, which I didn't want to do, but ENV variables won't work so no choice.
Give me another minute and I'll find the right call for the other env vars
Actually I would just recommend having some process to securely deliver the key to your service on deployment, it's not particularly different form using an env var, given that the env var would have to be delivered in some similar mechanism.
You can have keep your key in the well known location on your system $HOME/.config/gcloud/application_default_credentials.json on UNIX and set the env var for development, and I believe if you're running on GCE that key is provided, or there is a mechanism to provide that key in production.
Im having a similar problem, I'm using a p12 file in a fresh rails app. Getting the same error.
From within a simple ruby script I'm not getting this problem.
On my main rails app i'm not getting this problem(there was another problem i wanted to test on the fresh rails app)