Search code examples
phpgoogle-api-php-clientgoogle-apps-marketplace

Get "Not authorized to access the application ID" after using Google Apps Marketplace API


I have a web app and trying to find out which users in google domain have installed my app. I've tried to use code from here: Determine if a google user's domain has my marketplace app installed, but it doesn't works. I am still getting error "(403) Not authorized to access the application ID" in response.

Code:

            $private_key = file_get_contents('path_to_p.12_key');
            $service_account_name = '{service_acc_name}'; // name from developers console

            $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/appsmarketplace.license'), $private_key);

            $client = new Google_Client();
            $client->setAssertionCredentials($cred);

            $url = "https://www.googleapis.com/appsmarket/v2/licenseNotification/{appID}";

            $httpRequest = new Google_Http_Request($url, 'GET');
            $httpRequest->setBaseComponent($client->getBasePath());
            $httpRequest = $client->getAuth()->sign($httpRequest);

            try
            {
                    $result = $client->execute($httpRequest);
            }
            catch (Exception $e)
            {
                    echo $e->getMessage();
            }

I've also added https://www.googleapis.com/auth/appsmarketplace.license scope in project settings in developers console.

I can't get what's wrong.


Solution

  • OK, I've solved it. You must add all scopes to your service account object that used by your app (not just that scopes you are really want to use by service account):

    $cred = new Google_Auth_AssertionCredentials($service_account_name, array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/admin.directory.user.readonly', 'https://www.googleapis.com/auth/admin.directory.user', 'https://www.googleapis.com/auth/appsmarketplace.license'), $private_key);