# AuthorizationError error while using google-drive gem

**URL:** <https://www.ruby-forum.com/t/authorizationerror-error-while-using-google-drive-gem/254214>\
**Category:** Deployment\
**Created:** [February 5, 2020, 10:15pm UTC](https://www.ruby-forum.com/t/authorizationerror-error-while-using-google-drive-gem/254214 "2020-02-05T22:15:06Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yudechen](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/yudechen/32/2369_2.png) [@yudechen](https://www.ruby-forum.com/u/yudechen)\
**Post date:** [February 5, 2020, 10:15pm UTC](https://www.ruby-forum.com/t/authorizationerror-error-while-using-google-drive-gem/254214/1 "2020-02-05T22:15:07Z")

</div>

I am using `google_drive` gem.

This is `config.json` .

```auto
{
  "client_id": " *****.apps.googleusercontent.com",
  "client_secret": " *****",
  "scope": [
    "https://www.googleapis.com/auth/drive",
    "https://spreadsheets.google.com/feeds/"
  ],
  "refresh_token": " *****"
}

```

Code: Add row to google sheet.

```auto
session = GoogleDrive::Session.from_config("#{Rails.root.join('config/config.json')}")
ws = session.spreadsheet_by_key(ENV["GOOGLE_SHEET_KEY"]).worksheets[0]
num_rows = ws.num_rows

worksheet_row.each_with_index do |column, index|
  ws[num_rows + 1, index + 1] = column
end

```

It was working but recently I am getting this error.

```auto
Signet::AuthorizationError (Authorization failed. Server message:
{
  "error": "invalid_grant",
  "error_description": "Bad Request"
}):

```

Any idea?

---

<div class="post-metadata">

**Author:** ![Maniac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/13edae/32.png) [@Maniac](https://www.ruby-forum.com/u/Maniac)\
**Post date:** [March 19, 2020, 5:38pm UTC](https://www.ruby-forum.com/t/authorizationerror-error-while-using-google-drive-gem/254214/2 "2020-03-19T17:38:22Z")

</div>

Hello @yudechen,

I would suggest you to check the tips here:

> <https://github.com/googleapis/google-api-ruby-client/issues/84>
>
> Hello! Today I've faced an invalid grant error, and as I'm doing a service appli…cation I'm confused because there should not be any refresh tokens, only access tokens, right? https://developers.google.com/accounts/docs/OAuth2ServiceAccount
> 
> \`\`\` ruby
> WARN -- : Google::APIClient - Please provide :application\_name and :application\_version when initializing the client
> /home/lessless/.rvm/gems/ruby-2.0.0-p247/gems/signet-0.4.5/lib/signet/oauth\_2/client.rb:873:in \`fetch\_access\_token': Authorization failed. Server message: (Signet::AuthorizationError)
> {
> "error" : "invalid\_grant"
> }
> from /home/lessless/.rvm/gems/ruby-2.0.0-p247/gems/signet-0.4.5/lib/signet/oauth\_2/client.rb:888:in \`fetch\_access\_token!'
> from /home/lessless/projects/repaker/generator/\_plugins/lib/google\_client.rb:23:in \`fetch\_api\_token!'
> from /home/lessless/projects/repaker/generator/\_plugins/lib/google\_client.rb:8:in \`initialize'
> \`\`\`
> 
> here is my code
> 
> \`\`\` ruby
> 
> class GoogleClient
> attr\_reader :credentials, :client
> 
> def initialize(credentials)
> @credentials = credentials
> @client = Google::APIClient.new()
> fetch\_api\_token!
> end
> 
> 
> def fetch\_api\_token!
> # Load our credentials for the service account
> key = Google::APIClient::KeyUtils.load\_from\_pkcs12(credentials\['key\_file'\], credentials\['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 =\> credentials\['service\_account\_email'\],
> :signing\_key =\> key)
> 
> # Request a token for our service account
> @client.authorization.fetch\_access\_token!      
> end    
> 
> end
> 
> \`\`\`
> 
> also time is synced with ntp: sudo ntpdate ntp.ubuntu.com
> as https://developers.google.com/analytics/devguides/reporting/core/v2/gdataAuthentication suggests
