google api php client - Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" } -
i have prediction app that's based on google-api-php client version 1.0.0 latest. it's working in localhost environment. when deploy same exact app hosting server environment it's gave me following issue.
this error got hosting environment
fatal error: uncaught exception 'google_auth_exception' message 'error refreshing 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->refreshtokenrequest(array) #1 /home/predict/public_html/predict/index.php(40): google_auth_oauth2->refreshtokenwithassertion(object(google_auth_assertioncredentials)) #2 {main} thrown in /home/predict/public_html/predict/google/auth/oauth2.php on line 335 *curl , json extensions working under hosting environment.
this whole source code
<?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->setapplicationname("predition app"); $service = new google_service_prediction($client); if (isset($_session['service_token'])) { $client->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->setassertioncredentials($cred); if($client->getauth()->isaccesstokenexpired()) { $client->getauth()->refreshtokenwithassertion($cred); } $_session['service_token'] = $client->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->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->setinput($predictiondata); $predictionresult = $service->trainedmodels->predict($project,$id, $input,$optparams = array()); //print '<h3>prediction result:</h3><pre>' . print_r($result, true) . '</pre>'; $accuracymetaresult = $service->trainedmodels->get($project, $id, $optparams = array()); //print '<h3>classification accuracy result:</h3><pre>' . print_r($result, true) . '</pre>'; $stdout = new stdclass(); $stdout->datamodel = $predictionresult['id']; $stdout->outputlabel = $predictionresult['outputlabel']; $stdout->negative = $predictionresult['modeldata']['outputmulti'][0]['score']; $stdout->neutral = $predictionresult['modeldata']['outputmulti'][1]['score']; $stdout->positive = $predictionresult['modeldata']['outputmulti'][2]['score']; $stdout->modelcreated = $accuracymetaresult['created']; $stdout->trainingcompleted = $accuracymetaresult['trainingcomplete']; $stdout->trainingstatus = $accuracymetaresult['trainingstatus']; $stdout->modeltype = $accuracymetaresult['modeldata']['modelinfo']['modeltype']; $stdout->datamodelaccuracy = $accuracymetaresult['modeldata']['modelinfo']['classificationaccuracy']; echo json_encode($stdout); if ($client->getaccesstoken()) { $_session['token'] = $client->getaccesstoken(); }
i ave had hours of trying solve problem. turned out server's time couple of minutes ahead of google's time.
setting server time correct time fixed issue immediately.
hopefully helps :)
Comments
Post a Comment