title: Get Google Client API access token using CURL
body: <p>I have been trying for the last couple of days trying to get an access token and refresh token using Google Oauth2 but not getting anywhere.</p>
  
  <p>The documentation is rubbish and I can't find any examples that work.</p>
  
  <p>I have just started to use CURL trying to get the access code and refresh token but I keep getting errors with the response.</p>
  
  <p>I have tried using this</p>
  
  <pre><code>// init the resource
  $url = 'https://accounts.google.com/o/oauth2/token';
  $ch = curl_init($url);
  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  curl_setopt($ch, CURLOPT_FAILONERROR, false);  
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 
  
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/x-www-form-urlencoded',
      'Content-length: 0'
  ));
  
  curl_setopt($ch, CURLOPT_POSTFIELDS,
      'code=' . urlencode('AUTHORISATION_CODE') . '&amp;' .
      'client_id=' . urlencode('MY_CLINET_ID.apps.googleusercontent.com') . '&amp;' .
      'client_secret=' . urlencode('MY_CLIENT_SECRET') . '&amp;' .
      'redirect_uri=http://www.example.com/' . '&amp;' .
      'grant_type=authorization_code'
  ); 
  
  // execute
  $output = curl_exec($ch);
  
  echo $output;
  
  // free
  curl_close($ch);
  </code></pre>
  
  <p>but i just get this response</p>
  
  <pre><code>{ 
     "error" : "invalid_request", 
     "error_description" : "Required parameter is missing: grant_type"
  }
  </code></pre>
  
  <p>and I've tried running CURL from the command line</p>
  
  <pre><code>curl --data "code=AUTHORISATION_CODE&amp;client_id=MY_CLIENT_ID.apps.googleusercontent.com&amp;client_secret=MY_CLIENT_SECRET&amp;redirect_uri=http://www.example.com/&amp;grant_type=authorization_code" https://accounts.google.com/o/oauth2/token
  </code></pre>
  
  <p>but i just get a different response</p>
  
  <pre><code>{
    "error" : "invalid_client",
    "error_description" : "The OAuth client was not found."
  }
  </code></pre>
  
  <p>Why is it so difficult just to get an access token?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I don't know what's happening but I don't receive that error anymore and I get an access token but not a refresh token</p>
  
  <p>I've looked at this post <a href="https://stackoverflow.com/questions/10827920/google-oauth-refresh-token-is-not-being-received">Not receiving Google OAuth refresh token</a> and I've revoked access to my app to regenerate the refresh token  but using this code displays an error about an invalid code</p>