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

API Acces with PHP

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

Hello,

 

i try to change location info with api in PHP but if connect with service i am connect on bad account.

$client = new Google_Client();
$client->setApplicationName("Wshop Google MyBusiness Application");
$client->setAssertionCredentials( new Google_Auth_AssertionCredentials( $email, $scope, file_get_contents(buildAbsolutePath($_SERVER['SCRIPT_FILENAME'], '/../config/'.$credential)) ) );
$this->_GObject = new Google_Service_MyBusiness($client);

$account = $this->_GObject->accounts->listAccounts()->getAccounts();
$this->account_name = $account[0]->name;
$location = $this->_GObject->accounts_locations->listAccountsLocations($this->account_name)->getLocations();

I have no location.

 

But if i connect with web application il have result 

$client = new Google_Client();
            $client->setApplicationName("Wshop Google MyBusiness Application");
            $client->setClientId(ConfigGoogle::ACCOUNT_MAIL_MYBUSINESS);
            $client->setClientSecret(ConfigGoogle::ACCOUNT_SECRET_MYBUSINESS);
            $client->setScopes($scope);
            $client->setAuthConfigFile(buildAbsolutePath($_SERVER['SCRIPT_FILENAME'], '/../config/' . ConfigGoogle::ACCOUNT_KEY_MYBUSINESS));
            $client->setRedirectUri($this->url_redirect); 

            // For retrieving the refresh token
            $client->setAccessType("offline");
            $client->setApprovalPrompt("force"); // On valide automatiquement l'autorisation

            // Si on a pas le code d'autorisation on va le charger
            if ( $this->getCode() == '' ) {               
                // Recuperation de l'url pour obtenir le code d'authorisation
                $auth_url = $client->createAuthUrl();
                // Suppression de la session
                unset($_SESSION['access_token']);
                // Nous redirige vers notre page avec en parametre GET le code d'autorisation
                header('Location: ' . $auth_url);
                return;
            }

            // Si on a pas deja de token (utile pour les tests (rafraichissement de la page))
            if ( empty($_SESSION['access_token']) ) {
                // Maintenant avec le code on va générer un token
                $accessToken = $client->authenticate($this->getCode());

                // En enregistre le token récupéré
                $_SESSION['access_token'] = $accessToken;
            }

            // On definie le token d'acces pour le client
            $client->setAccessToken($_SESSION['access_token']);

            if ($client->isAccessTokenExpired()) {
                $client->refreshToken($client->getRefreshToken());
            }

I have all the location of my account. But i need to be connect on my google account and the tool is use by other customers.

 

Why with 2 ID client OAuth on the same user and project i connect on 2 different account ?

 

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Back O
October 2016

Re: API Acces with PHP

[ Edited ]
Google Employee
# 4
Google Employee

Hi @Back O,

 

I suppose you have read through the Accepted Solution for setting up your service account with the PHP client library. The following are the required steps you should take for the service account application to work with the Google My Business API:

  • You should make sure that you created a service account in your whitelisted project in Google API Console to access the API.
  • You should request user consent at least once for each Google Account you need to access the Google My Business data from. The end users will have to log in with their Google Accounts to manually authorize the application using OAuth 2.0 Authorization. They need to perform this process only once with an OAuth 2.0 client ID during the OAuth 2.0 installed application flow, OAuth 2.0 web server application flow (your other OAuth 2.0 client ID web app) or via OAuth 2.0 Playground. This process should be performed prior to using the service account application with its public/private key pair credentials and not while running it. You can verify or remove the authorized app from the Apps connected to your account page of each Google Account you requested user consent for.
  • When you prepare to make authorized API calls using the service account to access a particular end user’s Google My Business data, you specify the user to impersonate by specifying the email address of the Google Account that you have previously given user consent for. Please refer to the code snippet below for using a service account and specifying the user to impersonate with v1-branch of the Google API Client Library for PHP:

         $user_to_impersonate = 'user@example.org';

         $cred = new Google_Auth_AssertionCredentials(

           $client_email,

           array('https://www.googleapis.com/auth/plus.business.manage'),

           $key,

           'notasecret',                                 // Default P12 password

           'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type

           $user_to_impersonate

         );

 

You should be able to get your service account application to work after you’ve applied the above steps. Please let me know if you have further questions.

 

Thanks,

Terry

View solution in original post

Re: API Acces with PHP

Google Employee
# 2
Google Employee

Hi @Back O,

 

Please check out this Accepted Solution for help in setting up your service account with the PHP client library.

 

Thanks,

Terry

Re: API Acces with PHP

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hello Terry,

 

i haven't problem to connect. When i connect with AssertionCredential i have this result

 

All account : 
array(1) {
  [0]=>
  object(Google_Service_Mybusiness_Account)#309 (9) {
    ["internal_gapi_mappings":protected]=>
    array(0) {
    }
    ["accountName"]=>
    NULL
    ["name"]=>
    string(30) "accounts/115777864343058121833"
    ["role"]=>
    NULL
    ["stateType":protected]=>
    string(38) "Google_Service_Mybusiness_AccountState"
    ["stateDataType":protected]=>
    string(0) ""
    ["type"]=>
    string(8) "PERSONAL"
    ["modelData":protected]=>
    array(1) {
      ["state"]=>
      array(1) {
        ["status"]=>
        string(10) "UNVERIFIED"
      }
    }
    ["processed":protected]=>
    array(0) {
    }
  }
}

 

And no location (array(0) {})

 

But if i connect with Auth Application Web il have another account and all my location :

 

array(1) {
  [0]=>
  object(Google_Service_Mybusiness_Account)#88 (9) {
    ["internal_gapi_mappings":protected]=>
    array(0) {
    }
    ["accountName"]=>
    string(11) "Back Office"
    ["name"]=>
    string(30) "accounts/106945445877679377008"
    ["role"]=>
    NULL
    ["stateType":protected]=>
    string(38) "Google_Service_Mybusiness_AccountState"
    ["stateDataType":protected]=>
    string(0) ""
    ["type"]=>
    string(8) "PERSONAL"
    ["modelData":protected]=>
    array(1) {
      ["state"]=>
      array(1) {
        ["status"]=>
        string(22) "VERIFICATION_REQUESTED"
      }
    }
    ["processed":protected]=>
    array(0) {
    }
  }
}

And all location (array(100) {[0]=> object(Google_Service_Mybusiness_Location) ...})

 

 

Why with Auth_AssertionCredentials authentification i haven't the account number 106945445877679377008 like Application Web authentification ?

 

Thanks.

Marked as Best Answer.
Solution
Accepted by topic author Back O
October 2016

Re: API Acces with PHP

[ Edited ]
Google Employee
# 4
Google Employee

Hi @Back O,

 

I suppose you have read through the Accepted Solution for setting up your service account with the PHP client library. The following are the required steps you should take for the service account application to work with the Google My Business API:

  • You should make sure that you created a service account in your whitelisted project in Google API Console to access the API.
  • You should request user consent at least once for each Google Account you need to access the Google My Business data from. The end users will have to log in with their Google Accounts to manually authorize the application using OAuth 2.0 Authorization. They need to perform this process only once with an OAuth 2.0 client ID during the OAuth 2.0 installed application flow, OAuth 2.0 web server application flow (your other OAuth 2.0 client ID web app) or via OAuth 2.0 Playground. This process should be performed prior to using the service account application with its public/private key pair credentials and not while running it. You can verify or remove the authorized app from the Apps connected to your account page of each Google Account you requested user consent for.
  • When you prepare to make authorized API calls using the service account to access a particular end user’s Google My Business data, you specify the user to impersonate by specifying the email address of the Google Account that you have previously given user consent for. Please refer to the code snippet below for using a service account and specifying the user to impersonate with v1-branch of the Google API Client Library for PHP:

         $user_to_impersonate = 'user@example.org';

         $cred = new Google_Auth_AssertionCredentials(

           $client_email,

           array('https://www.googleapis.com/auth/plus.business.manage'),

           $key,

           'notasecret',                                 // Default P12 password

           'http://oauth.net/grant_type/jwt/1.0/bearer', // Default grant type

           $user_to_impersonate

         );

 

You should be able to get your service account application to work after you’ve applied the above steps. Please let me know if you have further questions.

 

Thanks,

Terry

API Acces with PHP

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

Hello,

 

thanks for your response. In the account management service should be added in the user authorization. It work now.

 

API Acces with PHP

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Hi Terry,

 

I'm following this recommendation to implement service account access.

 

What is the recommended way to retrieve the $user_to_impersonate following the OAuth2 authorization?  Is it possible to use the code returned from the callback to determine this (so I don't have to ask the user separately for his google account)?

 

TIA,

David