I'm trying to get info from my business via the Google My Business API but when I try to get the info via their google-api-php-client
I'm just getting the following message:
Fatal error: Uncaught Google_Service_Exception: {
"error": "unauthorized_client",
"error_description": "Client is unauthorized to retrieve access tokens using this method."
}
I've followed their documentation (here and here) but with no luck.
Code:
$client = new Google_Client();
$client->setAuthConfig($ga_keyfile);
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
$client->setClientId($clientID);
$client->setApplicationName($appname);
$client->setAccessType('offline'); // offline access
$client->setSubject("*********@gmail.com");
$client->setIncludeGrantedScopes(true);
$mybusiness = new Google_Service_MyBusiness($client);
echo "<pre>";
print_r($mybusiness->accounts->listAccounts()->getAccounts());
echo "</pre>";
Also I've noticed that this error only occurs when I do $client->setSubject("*********@gmail.com");
If I leave that out it makes requests but then there are no business location available.
I'm not 100% sure if it is because of missing user consent but after googling it looks like it. There is no documentation I've found that explains how to get user consent. Most of them explain what it is but not how to do it.
As 2nd test I've tried creating an auth url with $auth_url = $client->createAuthUrl();
and setting the redirect uri $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php');
(also tried setting it to nothing: '')
But then I get a redirect_uri_mismatch error. Which should be correct since you can't set a redirect uri in a service account (or the OAuth2 client of the service account for that matter)
So how can I fix this issue?