谷歌谓词API刷新令牌例外

时间:2013-04-18 11:23:00

标签: php google-api oauth-2.0 prediction

我必须为项目使用Google Predicate API,因此我首先播放“Hello Prediction”示例https://developers.google.com/prediction/docs/hello_world

我想使用Service account auth方法直接从我的Web服务器调用API

这是我的剧本,我总是得到一个

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>';
                }
            ?>

1 个答案:

答案 0 :(得分:0)

看起来我错过了范围中的预测

$client->setAssertionCredentials(new Google_AssertionCredentials(
                        EMAIL_ADDRESS,
                        array('https://www.googleapis.com/auth/devstorage.full_control','https://www.googleapis.com/auth/prediction'),
                        $key));

现在好了