使用Google_PredictionService Google API示例时出错500

时间:2014-01-16 21:10:03

标签: php google-api-php-client google-prediction

更新:

我的项目是能够提供基于网络的应用程序,允许访问者使用GoogleDrive上传/下载/删除文件。该项目要求它基于服务器,不需要用户提供的凭据就能执行这些功能。

简而言之,Web应用程序将文件存储在一个专用的google驱动器帐户上,而不是将文件存储在服务器上。

我研究了Google的开发者网站,并被指示使用以下示例作为起点,配置PHP应用程序以使用我设置的云端硬盘帐户。

我按照Google网页上的说明操作: https://code.google.com/p/google-api-php-client/wiki/OAuth2#Service_Accounts

当我执行此脚本时,我收到以下500错误:

  

PHP Catchable致命错误:参数3传递给   Google_HostedmodelsServiceResource :: predict()必须是。的实例   Google_Input,没有给出,被称为   /data/sites/scott/htdocs/dfs_development/drive/serviceAccount.php on   第62行并在中定义   /data/sites/scott/htdocs/dfs_development/apis/google-api-php-client/src/contrib/Google_PredictionService.php   在第36行

我在这里做错了什么?我不确定$ project变量应该保持什么,而且似乎predict()函数需要3个args,但我不知道它应该是什么。

这是我的代码,我从上面的URL获得。提前感谢您的回复。

require_once '../apis/google-api-php-client/src/Google_Client.php';
require_once '../apis/google-api-php-client/src/contrib/Google_PredictionService.php';

// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts

const CLIENT_ID = '##########.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '########@developer.gserviceaccount.com';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'pathto/secretlystored/######-privatekey.p12';

$client = new Google_Client();
$client->setApplicationName("My Google Drive");

// Set your cached access token. Remember to replace $_SESSION with a
// real database or memcached.
session_start();
if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/prediction'),
    $key)
);


$client->setClientId(CLIENT_ID);
$service = new Google_PredictionService($client);

// Prediction logic:
$id = 'dfslocalhost';
$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array('Je suis fatigue'));

$input = new Google_Input();
$input->setInput($predictionData);

$result = $service->hostedmodels->predict($id, $input); ## 500 ERROR occurs here.. 

print '<h2>Prediction Result:</h2><pre>' . print_r($result, true) . '</pre>';

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
  $_SESSION['token'] = $client->getAccessToken();
}

1 个答案:

答案 0 :(得分:1)

这是因为Google_PredictionService API尚未在您的google API控制台开发中激活。