title: Rails Google Client API - unable to exchange a refresh token for access token
body: <p>After struggling with some <a href="https://stackoverflow.com/questions/12520314/ssl-error-in-rails-3-2-3-while-using-google-oauth2-client-to-access-api">SSL issues</a> on my machine, I'm still trying to access a user's Blogger account through the Google Ruby Client API.  I'm using the following:</p>
  
  <ul>
  <li>Rails 3.2.3</li>
  <li>Ruby 1.9.3</li>
  <li>oauth2 (0.8.0)</li>
  <li>omniauth (1.1.1)</li>
  <li>omniauth-google-oauth2 (0.1.13)</li>
  <li>google-api-client (0.4.6)</li>
  </ul>
  
  <p>I can successfully authenticate users and access their blogs through the Google API at the time of authentication. When a user logs in, I store the <code>access_token</code> and <code>refresh_token</code> I receive from Google. and everything works great until the <code>access_token</code> expires.  I'm trying to build the functionality that exchanges the <code>refresh_token</code> for a new <code>access_token</code>, but keep coming up against walls.  Using the <a href="https://code.google.com/p/google-api-ruby-client/wiki/OAuth2" rel="nofollow noreferrer">client documentation</a> as an example, this is the code I'm using:</p>
  
  <pre><code>  client = Google::APIClient.new
    token_pair = auth.oauth_token   # access_token and refresh_token received during authentication
  
    # Load the access token if it's available
    if token_pair  
      client.authorization.update_token!(token_pair.to_hash)
    end            
  
    # Update access token if expired
    if client.authorization.refresh_token &amp;&amp; client.authorization.expired?
      client.authorization.fetch_access_token!
    end
  
    blogger = client.discovered_api('blogger', 'v3')
    result = client.execute(
        api_method: blogger.blogs.list_by_user,
        parameters: {'userId' =&gt; "self", 'fields' =&gt; 'items(description,id,name,url)'},
        headers: {'Content-Type' =&gt; 'application/json'})
  </code></pre>
  
  <p>This code works perfectly while the <code>access_token</code> is valid.  As soon as it expires though, I'm seeing 2 problems:</p>
  
  <ol>
  <li>Even though I know the token is expired (I've checked <code>expires_at</code> value in the database), <code>client.authorization.expired?</code> returns <code>false</code> -- is there a different way I can check the expiration of the token besides using the value in the database?</li>
  <li>When I force the execution of <code>client.authorization.fetch_access_token!</code> I get an <code>invalid_request</code> error.</li>
  </ol>
  
  <p>Can someone please let me know how I can exchange a <code>refresh_token</code> for a new <code>access_token</code> using the client API?  Even if you know how to do it in another language, that would be a big help as I can then try to Rubyfy it.  Thanks!!</p>