I have been trying to use the client library to work with GMB API, however I can't get past the point of: if (isset($_GET['code'])) {}, which comes back false every time.
Here is my code:
require_once 'google-api-php-client/vendor/autoload.php';
include_once "mybusiness/MyBusiness.php";
define('APPLICATION_NAME', 'Reviews Manager');
define('CREDENTIALS_PATH', '/credentials');
define('CLIENT_SECRET_PATH', 'client_secret.json');
$redirect_uri = 'http://localhost:8888';
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setAuthConfig(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']);
var_dump($accessToken); exit;
// 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));
}
And so on...
I have already went through all the motions of Creating Web Application Credentials, getting the JSON file, and have been able to complete all the authentication and create an API call to retrieve the account data using the Google Sign-in method outlined here: https://developers.google.com/my-business/content/implement-oauth#client-libraries
I am using the exact same data... the same client_id/secret_id, the same redirec_uri etc... but no matter what I do I can't get past the first part of getting the code mentioned above.
Could someone please help with this issue?
Thank you!
Have you reviewed the Using OAuth 2.0 For Web Server Applications documentation? It appears you're missing a few steps (namely creating an auth URL and redirecting to Google to obtain an auth code) which are required in order to obtain the $_GET['code'] value.
Thanks! That's exactly the resource I needed! Any chance you have good resources for how to use the GMB PHP library provided here https://developers.google.com/my-business/samples/ ?
Thanks again for your help!
Unfortunately I'm not aware of anything specific for PHP, however the client API will mirror the API reference. For instance, calls to accounts.admins.create would be executed as follows (simplified):
$admin = new Google_Service_MyBusiness_Admin();
$admin->setName($name);
$admin->setAdminName($adminName);
$myBusiness->accounts_admins->create($parent, $admin);
As you can see, each setter and method argument corresponds to a property defined in the API specification linked above.