user1971993
user1971993

Reputation:

Google Calendar Service Account API leads to invalid grant

I have took the sample code from

http://code.google.com/p/google-api-ruby-client/source/browse/service_account/analytics.rb?repo=samples

however, I have been trying to get it working but it just keeps on giving, no matter how hard I try ! Here is the error I keep on getting

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/signet-0.4.5/lib/signet/oauth_2/client.rb:875:in `fetch_access_token': Authorization failed.  Server message: (Signet::Authorization
Error)
{
  "error" : "invalid_grant"
}
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/signet-0.4.5/lib/signet/oauth_2/client.rb:888:in `fetch_access_token!'

How Do I solve this ?

Here is my complete code

    require 'google/api_client'
require 'date'
require 'openssl'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE

# Update these to match your own apps credentials
service_account_email = 'xxxxxx' # Email of service account
key_file = 'privatekey.p12' # File containing your private key
key_secret = 'notasecret' # Password to unlock private key
profileID = 'xxxxx' # Analytics profile ID.


client = Google::APIClient.new()

# Load our credentials for the service account
key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)
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/analytics.readonly',
  :issuer => service_account_email,
  :signing_key => key)

# Request a token for our service account
client.authorization.fetch_access_token!

analytics = client.discovered_api('analytics','v3')

startDate = DateTime.now.prev_month.strftime("%Y-%m-%d")
endDate = DateTime.now.strftime("%Y-%m-%d")

visitCount = client.execute(:api_method => analytics.data.ga.get, :parameters => { 
  'ids' => "ga:" + profileID, 
  'start-date' => startDate,
  'end-date' => endDate,
  'dimensions' => "ga:day,ga:month",
  'metrics' => "ga:visits",
  'sort' => "ga:month,ga:day" 
})

print visitCount.data.column_headers.map { |c|
  c.name  
}.join("\t")

visitCount.data.rows.each do |r|
  print r.join("\t"), "\n"
end

I have spent whole day to get this working. Please help. Thanks for your time.

Upvotes: 2

Views: 1585

Answers (1)

user1971993
user1971993

Reputation:

Solved it !

My Computer clock was for some reason 4 hours late ! Corrected System Time, it lead to no error.

1 - Make Your you solve the SSL Error if you have any by using the Google API Faq.

It took me whole day, so I'm going to leave my solutions so in future no one has to go crazy like I did.

account_email = '@developer.gserviceaccount.com' 
key_file = 'privatekey.p12' 
key_secret = 'notasecret' 
client = Google::APIClient.new()

key = Google::APIClient::KeyUtils.load_from_pkcs12(key_file, key_secret)

 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 => account_email,
   :signing_key => key)

# # Request a token for our service account
 client.authorization.fetch_access_token!
 service = client.discovered_api('calendar', 'v3')

Example to execute something shall be like in similar fashion.

  @event = {
    'summary' => '',
    'location' => 'this is where the location goes',
     'description' => 'desc',
    'start' => {
      'dateTime' => '2013-02-08T10:00:00.000-07:00' # Date with :- offset so (yyyy-mm-dd T hh:mm:ss.000-offset)
    },
    'end' => {
      'dateTime' => '2013-02-08T10:25:00.000-07:00' # Date with :- offset so (yyyy-mm-dd T hh:mm:ss.000-offset)
    }
  }

  # Create event using the json structure 
  result = client.execute(:api_method => service.events.insert,
                          :parameters => {'calendarId' => '**'},
                          :body => JSON.dump(@event),
                          :headers => {'Content-Type' => 'application/json'})

Upvotes: 5

Related Questions