致命错误:消息为“刷新OAuth2令牌时出错",消息为"{{error"",未捕获的异常"Google_Auth_Exception"; :"invalid_grant"; } [英] Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }

查看:308
本文介绍了致命错误:消息为“刷新OAuth2令牌时出错",消息为"{{error"",未捕获的异常"Google_Auth_Exception"; :"invalid_grant"; }的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Google-api-php客户端最新版本1.0.0的Prediction应用.它在localhost环境中完美运行.但是当我将相同的应用程序部署到托管服务器环境中时,出现了以下问题.

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.

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->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扩展在托管环境下正常运行.

*CURL and JSON extensions are working properly under Hosting environment.

<?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();
} 

推荐答案

我只有几个小时尝试解决此问题.原来,我服务器的时间比Google的时间早了几分钟.

I ave just had hours of trying to solve this problem. It turned out that my server's time was a couple of minutes ahead of google's time.

将我的服务器时间设置为正确的时间可立即解决此问题.

Setting my Server time to the correct time fixed this issue immediately.

希望这对某人有所帮助:)

Hopefully this helps someone :)

这篇关于致命错误:消息为“刷新OAuth2令牌时出错",消息为"{{error"",未捕获的异常"Google_Auth_Exception"; :"invalid_grant"; }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆