My Business
2.3K members online now
2.3K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

I had this working a while back and had to switch projects for a while, I came back to this and now it is not working.

 

Authentication seems to be working fine, Oauth works and gives me a list of google accounts to log in with, I can select an existing google account that has a google my business set up and I get the client data back just fine. What does not work is when I request accounts using listAccounts().

 

I am 99.9% sure I am whitelisted and I did have everything working about a month ago.

 

Please advise.

 

public function indexAction(){

$client = new \Google_Client();

$client->setAccessType('online'); // default: offline
$client->setApplicationName('googlemybusiness');
$client->setClientId('xxxxxxx');
$client->setClientSecret('xxxxxxx');
$scriptUri = "http://".$_SERVER["HTTP_HOST"].$_SERVER['PHP_SELF'];
$client->setRedirectUri('http://localhost/test');
$client->setScopes(array(
'https://www.googleapis.com/auth/plus.me',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'https://www.googleapis.com/auth/plus.business.manage',
));
//$client->setDeveloperKey('INSERT HERE'); // API key
require_once __DIR__ . "/../library/Google/mybusiness/Mybusiness.php";
$service = new \Google_Service_Mybusiness($client);

if (isset($_GET['logout'])) { // logout: destroy token
print '1';
unset($_SESSION['token']);
die('Logged out.');
}

if (isset($_GET['code']) && !isset($_SESSION['token'])) { // we received the positive auth callback, get the token and store it in session
print'2';
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}

if (isset($_SESSION['token'])) { // extract token from session and configure client
print'3';
$token = $_SESSION['token'];
$client->setAccessToken($token);
print_r($client);
//here is where it gives the 404, on list accounts
$test = $service->accounts->listAccounts();
$test2 = $service->accounts_locations->listAccountsLocations($test[0]['name']);
$test3 = $service->accounts_locations_reviews->listAccountsLocationsReviews($test2[0]['name']);


print_r($test);
print_r($test2);
print_r($test3);

print 'Done';
die;

 

1 Expert replyverified_user

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Google Employee
# 2
Google Employee

Hi @Kirk T,

 

Your OAuth 2.0 authorization in this script seems fine, though I don’t see anywhere in this script you request authorization from the user. I suppose you request user consent for a selected, existing Google Account in another separate script. What concerns me is the following lines of code in your script for accessing PHP object property value for getting the account_name for an account and the locationId for a location:

 

$test2 = $service->accounts_locations->listAccountsLocations($test[0]['name']);
$test3 = $service->accounts_locations_reviews->listAccountsLocationsReviews($test2[0]['name']);

 

It appears that you skipped some function calls. Please follow the code snippet below to list all reviews for the specified location with the Google API Client Library for PHP:

 

$mybusinessService = new Google_Service_Mybusiness($client);

 

// For testing purposes, selects the very first account in the accounts array

$accounts = $mybusinessService->accounts;

$accountsList = $accounts->listAccounts()->getAccounts();

$account = $accountsList[0];

 

// For testing purposes, selects the very first location in the locations array

$locations = $mybusinessService->accounts_locations;

$locationsList = $locations->listAccountsLocations($account->name)->getLocations();

$location = $locationsList[0];

 

// Lists all reviews for the specified location

$reviews = $mybusinessService->accounts_locations_reviews;

$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);

$reviewsList = $listReviewsResponse->getReviews();

 

If you are having problems with the OAuth 2.0 authorization using the Google API Client Library for PHP, you can read more about the solution with more code snippets in this thread.

 

Thanks,

Terry

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Thank you very much for the assistance,
I used the code you pasted and I get the exact same response.

Here is the code I am using now after the Token is added to the session:


if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
require_once __DIR__ . "/../library/Google/mybusiness/Mybusiness.php";
$mybusinessService = new \Google_Service_Mybusiness($client);

print_r($mybusinessService->version);
print '<br>';
print_r($mybusinessService->serviceName);

// For testing purposes, selects the very first account in the accounts array

$accounts = $mybusinessService->accounts;

$accountsList = $accounts->listAccounts()->getAccounts();

$account = $accountsList[0];



// For testing purposes, selects the very first location in the locations array

$locations = $mybusinessService->accounts_locations;

$locationsList = $locations->listAccountsLocations($account->name)->getLocations();

$location = $locationsList[0];



// Lists all reviews for the specified location

$reviews = $mybusinessService->accounts_locations_reviews;

$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);

$reviewsList = $listReviewsResponse->getReviews();



Here is the response I get in the browser:

v3
mybusiness
Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found
#0 /var/www/velocity/admin/vendor/google/apiclient/src/Google/Http/REST.php(41): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request), Object(Google_Client))
#1 /var/www/velocity/admin/vendor/google/apiclient/src/Google/Client.php(564): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
#2 /var/www/velocity/admin/vendor/google/apiclient/src/Google/Service/Resource.php(227): Google_Client->execute(Object(Google_Http_Request))
#3 /var/www/velocity/admin/app/library/Google/mybusiness/Mybusiness.php(485): Google_Service_Resource->call('list', Array, 'Google_Service_...')
#4 /var/www/velocity/admin/app/controllers/TestController.php(60): Google_Service_Mybusiness_Accounts_Resource->listAccounts()
#5 [internal function]: Velocity\Controllers\TestController->indexAction()
#6 [internal function]: Phalcon\Dispatcher->dispatch()
#7 /var/www/velocity/admin/public/index.php(44): Phalcon\Mvc\Application->handle()
#8 {main}

It still seems that oauth is working fine, and I can get the version and app name Just fine, it just breaks when asking for a list of accounts...

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Google Employee
# 4
Google Employee

Hi @Kirk T,

 

Judging from the source code you provided, I notice your source code doesn’t contain the below lines of code for refreshing the access token once it expires to enable offline access or requesting user consent again:

if (isset($_SESSION['token']) && $_SESSION['token']) {

 $client->setAccessToken($_SESSION['token']);

 if ($client->isAccessTokenExpired()) {

   $access_token= json_decode($_SESSION['token']);

   $client->refreshToken($access_token->refresh_token);

   unset($_SESSION['token']);

   $_SESSION['token'] = $client->getAccessToken();

 }

} else {

 $authUrl = $client->createAuthUrl();

}

 

Instead of using a PHP session variable to persist the access token and the refresh token, I suggest you store them in a file on your local disk. This way, your app will always be able to request a new access token when necessary without further human interactions for requesting user consent.

 

There could be other implementation errors in your source code that are under the hood and not spotted. I suggest you follow through the example in the code snippet below exactly to get your app to work before applying customizations:

include_once "Mybusiness.php";

 

define('APPLICATION_NAME', 'User Query - Google My Business API');

define('CREDENTIALS_PATH', '/path/to/credentials.json');

define('CLIENT_SECRET_PATH', '/path/to/client_secrets.json');

 

$redirect_uri = '<REDIRECT_URI_TO_YOUR_CURRENT_SCRIPT>';

 

$client = new Google_Client();

$client->setApplicationName(APPLICATION_NAME);

$client->setAuthConfigFile(CLIENT_SECRET_PATH);

$client->addScope("https://www.googleapis.com/auth/plus.business.manage");

$client->setRedirectUri($redirect_uri);

 

// For retrieving the refresh token

$client->setAccessType("offline");

$client->setApprovalPrompt("force");

 

/************************************************

We are going to create the Google My Business API

service, and query it.

************************************************/

$mybusinessService = new Google_Service_Mybusiness($client);

 

$credentialsPath = CREDENTIALS_PATH;

if (isset($_GET['code'])) {

  // Exchange authorization code for an access token.

  $accessToken = $client->authenticate($_GET['code']);

  // Store the credentials to disk.

  if (!file_exists(dirname($credentialsPath))) {

     mkdir(dirname($credentialsPath), 0700, true);

  }

  file_put_contents($credentialsPath, $accessToken);

  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

}

 

// Load previously authorized credentials from a file.

if (file_exists($credentialsPath)) {

  $accessToken = file_get_contents($credentialsPath);

  $client->setAccessToken($accessToken);

  // Refresh the token if it's expired.

  if ($client->isAccessTokenExpired()) {

     $client->refreshToken($client->getRefreshToken());

     file_put_contents($credentialsPath, $client->getAccessToken());

  }

} else {

  // Request authorization from the user.

  $authUrl = $client->createAuthUrl();

}

 

// For testing purposes, selects the very first account in the accounts array

$accounts = $mybusinessService->accounts;

$accountsList = $accounts->listAccounts()->getAccounts();

$account = $accountsList[0];

 

// For testing purposes, selects the very first location in the locations array

$locations = $mybusinessService->accounts_locations;

$locationsList = $locations->listAccountsLocations($account->name)->getLocations();

$location = $locationsList[0];

 

// Lists all reviews for the specified location

$reviews = $mybusinessService->accounts_locations_reviews;

$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);

$reviewsList = $listReviewsResponse->getReviews();

 

Please let me know whether you can get your app to list all accounts using an exact copy of my code snippet and I’ll follow with you on this issue.

 

Thanks,

Terry

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

I followed your instructions and made a brand new Apache host and a clean index.php file to use so there is no framework involved, I installed composer and installed the google api with it, I also included the My Business API.

I downloaded client_secrets.json file and created a credentials.json file and made sure they are owned by www-data, I added the code below and the code successfully updated the credentials.json with the token info and then the page errors out when calling accounts again. same error. Below is the code and the result:

 

 

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once('vendor/autoload.php');

include_once "Mybusiness.php";



define('APPLICATION_NAME', 'User Query - Google My Business API');

define('CREDENTIALS_PATH', 'credentials.json');

define('CLIENT_SECRET_PATH', 'client_secrets.json');



$redirect_uri = 'http://localhost/index.php';



$client = new \Google_Client();

$client->setApplicationName(APPLICATION_NAME);

$client->setAuthConfigFile(CLIENT_SECRET_PATH);

$client->addScope("https://www.googleapis.com/auth/plus.business.manage");

$client->setRedirectUri($redirect_uri);



// For retrieving the refresh token

$client->setAccessType("offline");

$client->setApprovalPrompt("force");



/************************************************

We are going to create the Google My Business API

service, and query it.

************************************************/

$mybusinessService = new \Google_Service_Mybusiness($client);



$credentialsPath = CREDENTIALS_PATH;

if (isset($_GET['code'])) {
print 'stop1<br>';
// Exchange authorization code for an access token.

$accessToken = $client->authenticate($_GET['code']);

// Store the credentials to disk.

if (!file_exists(dirname($credentialsPath))) {
print 'stop2<br>';
mkdir(dirname($credentialsPath), 0700, true);

}

file_put_contents($credentialsPath, $accessToken);

$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

}



// Load previously authorized credentials from a file.

if (file_exists($credentialsPath)) {
print 'stop3<br>';
$accessToken = file_get_contents($credentialsPath);

$client->setAccessToken($accessToken);

// Refresh the token if it's expired.

if ($client->isAccessTokenExpired()) {
print 'stop4<br>';

$client->refreshToken($client->getRefreshToken());

file_put_contents($credentialsPath, $client->getAccessToken());

}

} else {
print 'stop5<br>';
// Request authorization from the user.

$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);

die;
}


// For testing purposes, selects the very first account in the accounts array

print 'made it through auth<br>';
print 'stop6<br>';


$accounts = $mybusinessService->accounts;

$accountsList = $accounts->listAccounts()->getAccounts();

$account = $accountsList[0];



// For testing purposes, selects the very first location in the locations array

$locations = $mybusinessService->accounts_locations;

$locationsList = $locations->listAccountsLocations($account->name)->getLocations();

$location = $locationsList[0];



// Lists all reviews for the specified location

$reviews = $mybusinessService->accounts_locations_reviews;

$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);

$reviewsList = $listReviewsResponse->getReviews();


?>

 

 

 

The Result:

stop3
made it through auth
stop6
Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found' in /var/www/kellerselfdefense/vendor/google/apiclient/src/Google/Http/REST.php:79 Stack trace: #0 /var/www/kellerselfdefense/vendor/google/apiclient/src/Google/Http/REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request)) #1 /var/www/kellerselfdefense/vendor/google/apiclient/src/Google/Client.php(556): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request)) #2 /var/www/kellerselfdefense/vendor/google/apiclient/src/Google/Service/Resource.php(195): Google_Client->execute(Object(Google_Http_Request)) #3 /var/www/kellerselfdefense/Mybusiness.php(485): Google_Service_Resource->call('list', Array, 'Google_Service_...') #4 /var/www/kellerselfdefense/index.php(119): Google_Service_Mybusiness_Accounts_Resource->listAccounts() #5 {main} thrown in /var/www/kellerselfdefense/vendor/google/apiclient/src/Google/Http/REST.php on line 79

 

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Google Employee
# 6
Google Employee

Hi @Kirk T,

 

Please make sure that you installed the v1 of the Google API Client Library for PHP using Composer as instructed on the Installation page.

 

Please follow the Make a simple HTTP request step-by-step guide on the documentation website to use the OAuth 2.0 Playground to experiment with the Google My Business API and report back to me if you can list all accounts for the authenticated user.

 

You may be experiencing the 404 Method not found error that typically happens when you are using a different Project ID from the one we whitelisted for your company to access the Google My Business API.

 

I’ll reach out to you privately on a separate email thread for more details.

 

Thanks,

Terry

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

I ended up taking over this code. In the example provided, The credentials were in fact expired, and in some cases the wrong account was being used.

 

Code that works, for those that care:

 

public function googleReviewsAction(){
$client = $this->getGoogleClient(); //received from another function, meainingless in this context
$client->setAccessToken($this->getAccessToken()); //this abstracts away how we access our token, in this case it was session based
$myBusiness = new \Google_Service_Mybusiness($client);
$accounts = $myBusiness->accounts->listAccounts()->getAccounts();
if($accounts) foreach($accounts as $account){
$locations = $myBusiness->accounts_locations->listAccountsLocations($account->name)->getLocations();
if($locations) foreach($locations as $location){
$reviews = $myBusiness->accounts_locations_reviews->listAccountsLocationsReviews($location->name)->getReviews();
if($reviews) foreach($reviews as $review){
/**
* @var $review \Google_Service_Mybusiness_Review Object
*/
print "<h1>reviewer</h1>";
print '<pre>';
print_r($review->getReviewer());
print '</pre>';
print "<h4>{$review->comment}</h4>";
}


}


}

}

 

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭
hey Terry, thanks for giving the feedback. You were absolutely right and provided pretty good guidance given the amount of information provided. I'd suggest throwing it either in the session or in the database. What I did was append access tokens to the session, fetching a new one when the one was presumably expired. I do believe there could be a small error in the isTokenExpired, but what I will do in our case is just expire and delete the token if the one we have is expired or invalid, since they can be disabled outside of the control and UI of our application, then prompt them to enter a new one.

Great job!

Re: Error calling GET https://www.googleapis.com/v3/accounts: (404) Not Found

[ Edited ]
Google Employee
# 9
Google Employee

Hi @Kirk T & @Scott C,

 

I am glad that your problem has been resolved. I checked that the Project ID you sent to me is in fact the whitelisted one for your company, so this problem is not due to the 404 Method not found error. Please note that you should write your code to anticipate the possibility that a granted token might no longer work for various reasons, and obtain a new token when the existing token is deemed invalid.

 

Thanks,

The Google My Business API team