Thanks! Got passed that and ran into something else.
Below is my file as is (minus the client_id, service_account_name and key_file of course).
You’ll see I haven’t really changed anything except the couple variables that need to be changed.
I am getting this error:
<br />
<b>Fatal error</b>: Uncaught exception 'apiAuthException' with message 'Error refreshing the OAuth2 token, message: '{
"error" : "invalid_grant"
}'' in E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php:276
Stack trace:
#0 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php(253): apiOAuth2->refreshTokenRequest(Array)
#1 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php(206): apiOAuth2->refreshTokenWithAssertion()
#2 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\service\apiServiceResource.php(167): apiOAuth2->sign(Object(apiHttpRequest))
#3 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\contrib\apiPredictionService.php(134): apiServiceResource->__call('predict', Array)
#4 E:\xampp\htdocs\Google_Analytics_API\index_new.php(63): HostedmodelsServiceResource->predict('sample.language...', Object(Input))
#5 {main}
thrown in <b>E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php</b> on line <b>276</b><br />
<?php
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPredictionService.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 = 'client_id';
const SERVICE_ACCOUNT_NAME = 'service_account_name';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'key_file';
$client = new apiClient();
$client->setApplicationName("Google Prediction Sample");
// 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']);
print_r ($client);
}
// 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 apiAssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),
$key)
);
$client->setClientId(CLIENT_ID);
$service = new apiPredictionService($client);
// Prediction logic:
$id = 'sample.languageid';
$predictionData = new InputInput();
$predictionData->setCsvInstance(array('Je suis fatigue'));
$input = new Input();
$input->setInput($predictionData);
$result = $service->hostedmodels->predict($id, $input);
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();
}
?>
Any help is appreciated. I am continuing to research.
Thanks Chirag.
I did NOT have the prediction API enabled.
After doing that it did work but after working yesterday I ran it again this morning and the same error occurred.
Does that make sense at all?
Fatal error: Uncaught exception 'apiAuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php:276 Stack trace: #0 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php(253): apiOAuth2->refreshTokenRequest(Array) #1 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php(206): apiOAuth2->refreshTokenWithAssertion() #2 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\service\apiServiceResource.php(167): apiOAuth2->sign(Object(apiHttpRequest)) #3 E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\contrib\apiPredictionService.php(134): apiServiceResource->__call('predict', Array) #4 E:\xampp\htdocs\Google_Analytics_API\index.php(62): HostedmodelsServiceResource->predict('sample.language...', Object(Input)) #5 {main} thrown in E:\xampp\htdocs\Google_Analytics_API\google-api-php-client\src\auth\apiOAuth2.php on line 276
Thanks Chirag.
I just checked and I am running 5.3.8.
Any other ideas?
Thanks,
Andrew
Thanks Chirag.
I just checked and I am running 5.3.8.
Any other ideas?
Thanks,
Andrew
"error" : "invalid_grant"}