title: Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }
body: <p>I have a Prediction app that's based on Google-api-php client version 1.0.0 latest. it's perfectly working in the localhost environment. but when I deploy same exact app into the hosting server environment it's gave me following issue.</p>
  
  <h2>THIS IS THE ERROR I GOT FROM HOSTING ENVIRONMENT</h2>
  
  <pre><code>Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /home/predict/public_html/predict/Google/Auth/OAuth2.php:335 Stack trace: #0 /home/predict/public_html/predict/Google/Auth/OAuth2.php(294): Google_Auth_OAuth2-&gt;refreshTokenRequest(Array) #1 /home/predict/public_html/predict/index.php(40): Google_Auth_OAuth2-&gt;refreshTokenWithAssertion(Object(Google_Auth_AssertionCredentials)) #2 {main} thrown in /home/predict/public_html/predict/Google/Auth/OAuth2.php on line 335
  </code></pre>
  
  <p>*CURL and JSON extensions are working properly under Hosting environment.</p>
  
  <h2>THIS IS MY WHOLE SOURCE CODE</h2>
  
  <pre><code>&lt;?php
  session_start();
  
  set_include_path("Google" . PATH_SEPARATOR . get_include_path());
  require_once 'Client.php';
  require_once 'Service/Prediction.php';
  
  $client_id              = 'XXXXXXXXXX-uin70k09g5g6f05vqvdj9ch541c4spns.apps.googleusercontent.com';    
  $service_account_name   = 'XXXXXXXXXX-uin70k09g5g6f05vqvdj9ch541c4spns@developer.gserviceaccount.com';  
  $key_file_location      = 'APPKEY-d333c55a111a.p12';
  
  if ($client_id == 'XXXXXXXXXX-uin70k09g5g6f05vqvdj9ch541c4spns.googleusercontent.com'
      || !strlen($service_account_name)
      || !strlen($key_file_location)) {  
  }
  
  $client = new Google_Client();
  
  $client-&gt;setApplicationName("Predition App");
  $service = new Google_Service_Prediction($client);
  
  if (isset($_SESSION['service_token'])) {
    $client-&gt;setAccessToken($_SESSION['service_token']);
  }
  
  $key = file_get_contents($key_file_location);
  $cred = new Google_Auth_AssertionCredentials(
      $service_account_name,`enter code here`
      array('https://www.googleapis.com/auth/prediction'),
      $key
  );
  
  $client-&gt;setAssertionCredentials($cred);
  
  if($client-&gt;getAuth()-&gt;isAccessTokenExpired()) {    
    $client-&gt;getAuth()-&gt;refreshTokenWithAssertion($cred); 
  }
  
  $_SESSION['service_token'] = $client-&gt;getAccessToken();
  
  // Prediction logic:
  $id             = 'datamodel';
  $project        = "XXXXXXXXXX";
  
  $input          = $_REQUEST['input'];
  
  //$predictionData = new Google_InputInput();  //Depricated method in Google_api_php_client_version1.0.0      // W.P.Roshan
  $predictionData = new Google_Service_Prediction_InputInput();
  $predictionData-&gt;setCsvInstance(array($input));
  
  //$input = new Google_Input();                //Depricated method in Google_api_php_client_version1.0.0 // W.P.Roshan
  $input = new Google_Service_Prediction_Input();
  $input-&gt;setInput($predictionData);
  
  $PredictionResult = $service-&gt;trainedmodels-&gt;predict($project,$id, $input,$optParams = array());
  //print '&lt;h3&gt;Prediction Result:&lt;/h3&gt;&lt;pre&gt;' . print_r($result, true) . '&lt;/pre&gt;';
  
  $AccuracyMetaResult = $service-&gt;trainedmodels-&gt;get($project, $id, $optParams = array());    
  //print '&lt;h3&gt;Classification Accuracy Result:&lt;/h3&gt;&lt;pre&gt;' . print_r($result, true) . '&lt;/pre&gt;';
  
  $stdout                     = new stdClass();
  $stdout-&gt;dataModel          = $PredictionResult['id'];
  $stdout-&gt;outputLabel        = $PredictionResult['outputLabel'];
  $stdout-&gt;negative           = $PredictionResult['modelData']['outputMulti'][0]['score'];
  $stdout-&gt;neutral            = $PredictionResult['modelData']['outputMulti'][1]['score'];
  $stdout-&gt;positive           = $PredictionResult['modelData']['outputMulti'][2]['score'];
  $stdout-&gt;modelCreated       = $AccuracyMetaResult['created'];
  $stdout-&gt;trainingCompleted  = $AccuracyMetaResult['trainingComplete'];
  $stdout-&gt;trainingStatus     = $AccuracyMetaResult['trainingStatus'];
  $stdout-&gt;modelType          = $AccuracyMetaResult['modelData']['modelInfo']['modelType'];
  $stdout-&gt;dataModelAccuracy  = $AccuracyMetaResult['modelData']['modelInfo']['classificationAccuracy'];
  
  echo json_encode($stdout);
  
  if ($client-&gt;getAccessToken()) {
    $_SESSION['token'] = $client-&gt;getAccessToken();
  } 
  </code></pre>