Google refresh token getting invalid grant error

I'm trying to use google analytics api for my account. So at first i allow the application access with offline refresh token. Buy when I try to refresh the token I get "Invalid Grant" error. Tried to refresh both with the php client library and manually with curl.

this is manually with curl:

$oauth2token_url = "";
    $clienttoken_post = array(
    "client_id" => '',
    "client_secret" => 'xxxxxxxxxxxxx');

        $clienttoken_post["refresh_token"] = $refresh_token;
        $clienttoken_post["grant_type"] = "refresh_token";

    $curl = curl_init($oauth2token_url);

    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

    $json_response = curl_exec($curl);

    $authObj = json_decode($json_response);



the refresh token i got with a previous call using the simple.php example:

require_once '../../src/apiClient.php';
require_once '../../src/contrib/apiAnalyticsService.php';

$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");

// Visit to generate your
// client id, client secret, and to register your redirect uri.

if (isset($_GET['logout'])) {

if (isset($_GET['code'])) {
  $_SESSION['token'] = $client->getAccessToken();
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);

if (isset($_SESSION['token'])) {
//    echo $_SESSION['token'];
echo $client->getAccessToken()


if anyone is interested i finally solved it after a few hours:

I went to oauth playground: in the right, there is a settings button. selected "Use your own OAuth credentials". took the refresh token from there, and it works with no problem.

If anyone else runs into this problem ...

We had an issue where the same post request would not work initially, but work thereafter.

For us it looked like google was super picky about the iat time -- try subtracting 5 minutes from it.

Need Your Help

Managing Multiple NSMutableURLRequest and synchronous NSURLConnection in iOS

ios nsurlconnection nsmutableurlrequest

I am downloading the data from the web service API.In my project i have to download data using different APIs in so many places.So every where Iam calling web service, this leads to complexity of c...

How do i add fileds dynamically using codeIgniter with script?

javascript php codeigniter

i need add fields dynamically in my view form. i have a script function for that.. but i don't know how to include in that script into codeIgniter.