0

I'm getting only access_token when I use this code.

url = URI("https://www.googleapis.com/oauth2/v4/token")

    secrets = {
      code: params[:code],
      client_id: Rails.application.secrets.google[:client_id],
      client_secret: Rails.application.secrets.google[:client_secret],
      grant_type: "authorization_code",
      redirect_uri: Rails.application.secrets.front_end_domain
    }

    response = Net::HTTP.post_form(url, secrets)

    json_response = JSON.parse(response.read_body)

How to change the code so that I can get the refresh_token every time?

Brian Hobbs
  • 59
  • 1
  • 8

1 Answers1

1

To get a refresh token, you need to pass,

 access_type: 'offline'

Did you get a pop up for approving the permissions when fetching the code on the client-side? You need to make sure that you pass access_type: 'offline' in the client-side when the pop up comes for authorization. Unless this is passed, and you have given consent to the permissions pop up, Google will not provide the refresh_token.

Gautam
  • 1,754
  • 1
  • 14
  • 22
  • Thanks for the answer. I tried to add this, but it's still not getting the refresh token. – Brian Hobbs Feb 26 '20 at 13:59
  • I have updated the answer if you provide more details, I can help you better. – Gautam Feb 26 '20 at 15:01
  • Google's policy on this changed sometime along 2022. For full details, refer to the most recent comments at https://stackoverflow.com/a/55164583/1091231 . – pasqal Jun 27 '23 at 14:35