【问题标题】:google predicate API Refresh token exception谷歌谓词 API 刷新令牌异常
【发布时间】:2013-04-18 11:22:59
【问题描述】:

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

我想使用服务帐户身份验证方法直接从我的网络服务器调用 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>';
                }
            ?>

【问题讨论】:

    标签: php google-api oauth-2.0 prediction


    【解决方案1】:

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

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

    现在好了

    【讨论】:

      猜你喜欢
      • 2016-01-07
      • 2021-05-06
      • 2016-03-29
      • 2017-04-19
      • 1970-01-01
      • 2019-01-11
      • 2015-05-17
      • 2013-07-20
      • 2014-03-31
      相关资源
      最近更新 更多