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

What seems to be missing from the documentation...

[ Edited ]
Follower ✭ ☆ ☆
# 1
Follower ✭ ☆ ☆

Assuming you've figured out the oauth...

 

All the REST calls need basically the same thing: a curl call. The parameters change, but anyone can handle the changes. The issue is crossing the 't's and dotting the 'i's. What I need to move forward is just an example of a complete curl call that should work. (Of course, there will be places where you fill in your information.)

 

Ideally in a lot of languages. Faster than whole APIs. Also spending time guessing at things is such a waste. Whenever someone asks a question and you say read this Documents... just give a complete (generic is fine) example.

 

Right now I am fighting just getting the list of accounts for the logged in user. Seems the easiest thing.

 

To make progress I just need a piece of code that should work -- this way I can know if it's me or if there's something wrong with the system.

 

--

 

Anyway -- if anyone has a snippet of php code to make the curl call to a GMB REST API, I would be grateful.

 

 

 

 

 

 

 

1 Expert replyverified_user

Re: What seems to be missing from the documentation...

Google Employee
# 2
Google Employee

Hi @Brad E,

 

We strongly encourage you to use the PHP client library we provide when interacting with Google's OAuth 2.0 endpoints for the security implications of getting the implementation correct. After you have obtained a valid OAuth 2.0 client ID, you can follow the example in the below code snippet for using OAuth 2.0 Authorization in a PHP command-line application and persisting the refresh token with the Google API Client Library for PHP:

 

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');

 

if (php_sapi_name() != 'cli') {

 throw new Exception('This application must be run on the command line.');

}

 

$redirect_uri = 'urn:ietf:wg:oauth:2.0:oob';

 

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

 Make an API request on behalf of a user. In

 this case we need to have a valid OAuth 2.0

 token for the user, so we need to send them

 through a login flow. To do this we need some

 information from our API console project.

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

$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);

 

// Load previously authorized credentials from a file.

$credentialsPath = CREDENTIALS_PATH;

if (file_exists($credentialsPath)) {

 $accessToken = file_get_contents($credentialsPath);

} else {

 // Request authorization from the user.

 $authUrl = $client->createAuthUrl();

 printf("Open the following link in your browser:\n%s\n", $authUrl);

 print 'Enter verification code: ';

 $authCode = trim(fgets(STDIN));

 

 // Exchange authorization code for an access token.

 $accessToken = $client->authenticate($authCode);

 

 // Store the credentials to disk.

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

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

 }

 file_put_contents($credentialsPath, $accessToken);

 printf("Credentials saved to %s\n", $credentialsPath);

}

$client->setAccessToken($accessToken);

 

// Refresh the token if it's expired.

if ($client->isAccessTokenExpired()) {

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

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

}

 

// Lists all accounts for the authenticated user.

$accounts = $mybusinessService->accounts;

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

 

I hope this helps solve your problem. Please let me know if you need a pure cURL script that serves the same purpose.

 

Thanks,

Terry

Re: What seems to be missing from the documentation...

Follower ✭ ☆ ☆
# 3
Follower ✭ ☆ ☆
Thanks a lot. Once we straightened out an issue with the whitelisting project I was able to get REST via curl working right away.

I tried using the PHP client library, but it was using the wrong URL -- I assume something is not quite right with composer.

The nice thing about curl calls isI can watch the network stack pretty easily when things are going wrong.

Again, I appreciate your help