title: OAuth2 what do I do with the response code from Google?
body: <p>So I logged in with Google and it hits my callback url with a code parameter.</p>
  
  <p>Here is how I initiate my client, I'm using oauth2 gem.</p>
  
  <pre><code>  def oauth_client(channel_name)
      file = YAML.load_file("#{Rails.root}/config/oauth_credentials.yml")
      client_id = file['oauth_credentials'][channel_name]['client_id']
      client_secret = file['oauth_credentials'][channel_name]['secret']
      site = file['oauth_credentials'][channel_name]['site']
  
      OAuth2::Client.new(client_id, client_secret, site: site, authorize_url: "/o/oauth2/auth", connection_opts: { params: { scope: "https://www.googleapis.com/auth/adsense https://www.googleapis.com/auth/analytics.readonly" } })
    end
  
    def oauth_url_for(channel_name)
      client = oauth_client(channel_name)
      client.auth_code.authorize_url(:redirect_uri =&gt; oauth_callback_url(channel: channel_name))
    end
  </code></pre>
  
  <p>Here's my controller</p>
  
  <pre><code>class Oauth2Controller &lt; ApplicationController
    include ApplicationHelper
  
    def callback
      token = oauth_client(params[:channel]).auth_code.get_token(params[:code], :redirect_uri =&gt; oauth_callback_url(channel: params[:channel]))
      current_user.connections.create!(channel: params[:channel], token: token)
      render text: request.inspect
    end
  end
  </code></pre>
  
  <p>Unfortunately I can't get_token due to a response from google saying  The page you requested is invalid.</p>