pDEV
pDEV

Reputation: 71

googleapi "oauth2/v4/token" is not returning "refresh_token"

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?

Upvotes: 0

Views: 218

Answers (1)

Gautam
Gautam

Reputation: 1812

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.

Upvotes: 1

Related Questions