Search code examples
ruby-on-railsoauth-2.0google-apiomniauthgoogle-api-client

rails refreshing scope request for google api


I have a rails app with google-api-client and omniauth-google-oauth2 gems. I would like to integrate google calendar. I tried to do the authentication with the code below, but it failed.

The problem is when I change the scope let's say to: scope: ['email'] or even to scope: ['hahahaha'] I get the same response from google which is shown below the code. After playing with it for a while I gave up and waited some time then tried again and I got a new error response. I tried to change the code but I got stuck again this time with another error. Is there a time limit or why the response doesn't het updated right after changing the code?

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :google_oauth2, ENV['GOOGLE_API_KEY'], ENV['GOOGLE_API_SECRET'], {
    scope: ['email, profile, calendar'],
    access_type: 'offline'
  }
end

error message from google:

Error: invalid_scope

Some requested scopes were invalid. {valid=https://www.googleapis.com/auth/calendar, [invalid= profile, https://www.googleapis.com/auth/email,]}


Solution

  • According to omniauth-google-oauth2 docs scope should be a string with comma separated scopes. But you're using an array in your code. So scope: "email, profile, calendar" should work.

    As to response changing over time, do you use Spring in you Rails app? Spring allows you to load Rails app faster by preloading it during the first run. Then, when you run rails again, it uses this preloaded app. After changing app configuration you have to restart application for the changes to come into effect. If you just restart rails it won't work, because Spring is still running an app in memory. So, if you use Spring you have to restart it as well after changing app configuration, like so:

    spring stop
    rails s