使用Google _ PredictionService Google API示例时出现错误500



更新:

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

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

我研究了谷歌的开发者网站,并被引导使用下面的例子作为起点,来配置PHP应用程序以使用我设置的驱动器帐户。

我遵循了谷歌页面上的说明:https://code.google.com/p/google-api-php-client/wiki/OAuth2#Service_Accounts

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

PHP可捕获的致命错误:参数3传递给Google_HostedmodelsServiceResource::predict()必须是的实例Google_Input,未给定,已调用/data/sites/scott/htdocs/dfs_development/drive/serviceAccount.php第62行,定义于/data/sites/scott/htdocs/dfs_development/apis/google-api-php-client/src/controb/google_PrecdictionService.php在线36

我做错了什么?我不确定$project变量应该包含什么,predict()函数似乎需要3个参数,但我不知道它应该是什么。

这是我从上面的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();
}

这是因为Google _ PredictionService API尚未在您的Google API控制台开发中激活。

相关内容

最新更新