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

access token

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

hi 

 
i get PHP Fatal error:  Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' when trying to access my account whith this script.
 
require_once 'src/Google/autoload.php'; 
require_once 'Mybusiness.php';  
define('APPLICATION_NAME', 'google_api');
define('CREDENTIALS_PATH', 'credentials.json');
define('CLIENT_SECRET_PATH', 'client_secret2.json');
$redirect_uri = 'urn:ietf:wg:oauth:2.0:oob';
 $client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setRedirectUri($redirect_uri);
$client->setAccessType("offline");
$client->setApprovalPrompt("force"); 
$mybusinessService = new Google_Service_Mybusiness($client);
$credentialsPath = CREDENTIALS_PATH;
if (file_exists($credentialsPath)) {
  $accessToken = file_get_contents($credentialsPath);
  $client->setAccessToken($accessToken);
  if ($client->isAccessTokenExpired()) {
    $client->refreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, $client->getAccessToken());
  }
} else {
  $authUrl = $client->createAuthUrl();
}
$rslt_data=$mybusinessService->accounts_locations->listAccountsLocations('accounts/xxxxxxxxxxxxxxxxxxxxx');
 
echo  $rslt_data;
 
thx for the help
1 Expert replyverified_user

Re: access token

Google Employee
# 2
Google Employee

Hi @Mario R,

 

After looking at the code you provided, I believe you need to make slight changes in your code when you refresh the token if it's expired. Since PHP quickstart recently updated with an improved method, so I suggest you please follow the code below to see if it fixes the issue:

  

//Refresh the token if it's expired
if (file_exists($credentialsPath)) {
 $accessToken = file_get_contents($credentialsPath);
 $client->setAccessToken($accessToken);
 if ($client->isAccessTokenExpired()) {
   $client->refreshToken($client->getRefreshToken());
   file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
 }

 

Since the PHP client library doesn’t currently support the display of detailed error messages, you should follow the Make a simple HTTP request step-by-step guide on the Google Developers site to learn how to use the OAuth 2.0 Playground for troubleshooting errors with detailed error messages when sending requests with data via the Google My Business API.

 

Thanks,

The Google My Business API team