0
我必須使用谷歌謂詞API的項目,所以我米打「你好預測」樣品第一https://developers.google.com/prediction/docs/hello_world谷歌謂語API刷新令牌例外
我想利用服務帳戶身份驗證方法,以便直接調用API從我的Web服務器
這裏是我的腳本,我總是得到
Google_AuthException Object
(
[message:protected] => Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}
有什麼建議?
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once 'google-api-php-client/Google_Client.php';
require_once 'google-api-php-client/contrib/Google_PredictionService.php';
session_start();
$client = new Google_Client();
define("CLIENT_ID", 'xxxxxxxxxxxxxx.apps.googleusercontent.com');
define("SERVICE_ACCOUNT_NAME", 'Prediction API Project');
define("KEY_FILE", 'google-api-php-client/xxxxxxxxcd-privatekey.p12');
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/devstorage.full_control'),
$key));
$client->setClientId(CLIENT_ID);
$predictionService = new Google_PredictionService($client);
$trainedmodels = $predictionService->trainedmodels;
$id = "languageidentifier";
$predictionText = "Je suis vraiment très très fatigué";
$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array($predictionText));
$input = new Google_Input();
$input->setInput($predictionData);
try{
$result = $predictionService->trainedmodels->predict($id, $input);
print("<h2>Prediction Result:</h2>");
print_r($result);
}
catch (Exception $e)
{
echo '<pre>';
print_r($e);
echo '</pre>';
}
?>